clean up
This commit is contained in:
parent
fb32f3532e
commit
c594c93559
4 changed files with 183 additions and 173 deletions
34
common.go
34
common.go
|
@ -1,7 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"html/template"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -15,3 +19,33 @@ var (
|
|||
},
|
||||
}
|
||||
)
|
||||
|
||||
func generateStrongRandomString(length int) string {
|
||||
bytes := make([]byte, length)
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Error generating random string: %v", err)
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(bytes)[:length]
|
||||
}
|
||||
|
||||
// Checks if the URL already includes a protocol
|
||||
func hasProtocol(url string) bool {
|
||||
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
|
||||
}
|
||||
|
||||
// Checks if the domain is a local address
|
||||
func isLocalAddress(domain string) bool {
|
||||
return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.")
|
||||
}
|
||||
|
||||
// Ensures that HTTP or HTTPS is befor the adress if needed
|
||||
func addProtocol(domain string) string {
|
||||
if hasProtocol(domain) {
|
||||
return domain
|
||||
}
|
||||
if isLocalAddress(domain) {
|
||||
return "http://" + domain
|
||||
}
|
||||
return "https://" + domain
|
||||
}
|
||||
|
|
149
config.go
Normal file
149
config.go
Normal file
|
@ -0,0 +1,149 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
func initConfig() error {
|
||||
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
|
||||
return createConfig()
|
||||
}
|
||||
|
||||
fmt.Println("Configuration file already exists.")
|
||||
config = loadConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
func createConfig() error {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("Configuration file not found.")
|
||||
fmt.Print("Do you want to use default values? (yes/no): ")
|
||||
useDefaults, _ := reader.ReadString('\n')
|
||||
|
||||
if strings.TrimSpace(useDefaults) != "yes" {
|
||||
fmt.Print("Enter port (default 5000): ")
|
||||
portStr, _ := reader.ReadString('\n')
|
||||
if portStr != "\n" {
|
||||
port, err := strconv.Atoi(strings.TrimSpace(portStr))
|
||||
if err != nil {
|
||||
config.Port = 5000
|
||||
} else {
|
||||
config.Port = port
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Print("Enter your domain address (default localhost): ")
|
||||
domain, _ := reader.ReadString('\n')
|
||||
if domain != "\n" {
|
||||
config.Domain = strings.TrimSpace(domain)
|
||||
}
|
||||
} else {
|
||||
config = defaultConfig
|
||||
}
|
||||
|
||||
if config.AuthCode == "" {
|
||||
config.AuthCode = generateStrongRandomString(64)
|
||||
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
|
||||
}
|
||||
|
||||
saveConfig(config)
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveConfig(config Config) {
|
||||
cfg := ini.Empty()
|
||||
sec := cfg.Section("")
|
||||
sec.Key("Port").SetValue(strconv.Itoa(config.Port))
|
||||
sec.Key("AuthCode").SetValue(config.AuthCode)
|
||||
sec.Key("PeerID").SetValue(config.PeerID)
|
||||
|
||||
peers := strings.Join(config.Peers, ",")
|
||||
sec.Key("Peers").SetValue(peers)
|
||||
sec.Key("Domain").SetValue(config.Domain)
|
||||
|
||||
err := cfg.SaveTo(configFilePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing to config file:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfig() Config {
|
||||
cfg, err := ini.Load(configFilePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening config file: %v", err)
|
||||
}
|
||||
|
||||
port, err := cfg.Section("").Key("Port").Int()
|
||||
if err != nil || port == 0 {
|
||||
port = 5000 // Default to 5000 if not set or error
|
||||
}
|
||||
|
||||
peersStr := cfg.Section("").Key("Peers").String()
|
||||
var peers []string
|
||||
if peersStr != "" {
|
||||
peers = strings.Split(peersStr, ",")
|
||||
for i, peer := range peers {
|
||||
peers[i] = addProtocol(peer)
|
||||
}
|
||||
}
|
||||
|
||||
domain := cfg.Section("").Key("Domain").String()
|
||||
if domain == "" {
|
||||
domain = "localhost" // Default to localhost if not set
|
||||
}
|
||||
|
||||
config = Config{
|
||||
Port: port,
|
||||
AuthCode: cfg.Section("").Key("AuthCode").String(),
|
||||
PeerID: cfg.Section("").Key("PeerID").String(),
|
||||
Peers: peers,
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func startFileWatcher() {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer watcher.Close()
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||
log.Println("Modified file:", event.Name)
|
||||
configLock.Lock()
|
||||
config = loadConfig()
|
||||
configLock.Unlock()
|
||||
// Perform your logic here to handle the changes in the config file
|
||||
}
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Println("Error:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
err = watcher.Add(configFilePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
151
init.go
151
init.go
|
@ -1,19 +1,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -67,145 +58,3 @@ func main() {
|
|||
|
||||
runServer()
|
||||
}
|
||||
|
||||
func initConfig() error {
|
||||
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
|
||||
return createConfig()
|
||||
}
|
||||
|
||||
fmt.Println("Configuration file already exists.")
|
||||
config = loadConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
func createConfig() error {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("Configuration file not found.")
|
||||
fmt.Print("Do you want to use default values? (yes/no): ")
|
||||
useDefaults, _ := reader.ReadString('\n')
|
||||
|
||||
if strings.TrimSpace(useDefaults) != "yes" {
|
||||
fmt.Print("Enter port (default 5000): ")
|
||||
portStr, _ := reader.ReadString('\n')
|
||||
if portStr != "\n" {
|
||||
port, err := strconv.Atoi(strings.TrimSpace(portStr))
|
||||
if err != nil {
|
||||
config.Port = 5000
|
||||
} else {
|
||||
config.Port = port
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Print("Enter your domain address (default localhost): ")
|
||||
domain, _ := reader.ReadString('\n')
|
||||
if domain != "\n" {
|
||||
config.Domain = strings.TrimSpace(domain)
|
||||
}
|
||||
} else {
|
||||
config = defaultConfig
|
||||
}
|
||||
|
||||
if config.AuthCode == "" {
|
||||
config.AuthCode = generateStrongRandomString(64)
|
||||
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
|
||||
}
|
||||
|
||||
saveConfig(config)
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveConfig(config Config) {
|
||||
cfg := ini.Empty()
|
||||
sec := cfg.Section("")
|
||||
sec.Key("Port").SetValue(strconv.Itoa(config.Port))
|
||||
sec.Key("AuthCode").SetValue(config.AuthCode)
|
||||
sec.Key("PeerID").SetValue(config.PeerID)
|
||||
|
||||
peers := strings.Join(config.Peers, ",")
|
||||
sec.Key("Peers").SetValue(peers)
|
||||
sec.Key("Domain").SetValue(config.Domain)
|
||||
|
||||
err := cfg.SaveTo(configFilePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing to config file:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfig() Config {
|
||||
cfg, err := ini.Load(configFilePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening config file: %v", err)
|
||||
}
|
||||
|
||||
port, err := cfg.Section("").Key("Port").Int()
|
||||
if err != nil || port == 0 {
|
||||
port = 5000 // Default to 5000 if not set or error
|
||||
}
|
||||
|
||||
peersStr := cfg.Section("").Key("Peers").String()
|
||||
var peers []string
|
||||
if peersStr != "" {
|
||||
peers = strings.Split(peersStr, ",")
|
||||
}
|
||||
|
||||
domain := cfg.Section("").Key("Domain").String()
|
||||
if domain == "" {
|
||||
domain = "localhost" // Default to localhost if not set
|
||||
}
|
||||
|
||||
config = Config{
|
||||
Port: port,
|
||||
AuthCode: cfg.Section("").Key("AuthCode").String(),
|
||||
PeerID: cfg.Section("").Key("PeerID").String(),
|
||||
Peers: peers,
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func generateStrongRandomString(length int) string {
|
||||
bytes := make([]byte, length)
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Error generating random string: %v", err)
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(bytes)[:length]
|
||||
}
|
||||
|
||||
func startFileWatcher() {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer watcher.Close()
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||
log.Println("Modified file:", event.Name)
|
||||
configLock.Lock()
|
||||
config = loadConfig()
|
||||
configLock.Unlock()
|
||||
// Perform your logic here to handle the changes in the config file
|
||||
}
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Println("Error:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
err = watcher.Add(configFilePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"encoding/xml"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type OpenSearchDescription struct {
|
||||
|
@ -21,27 +20,6 @@ type URL struct {
|
|||
Template string `xml:"template,attr"`
|
||||
}
|
||||
|
||||
// Checks if the URL already includes a protocol
|
||||
func hasProtocol(url string) bool {
|
||||
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
|
||||
}
|
||||
|
||||
// Checks if the domain is a local address
|
||||
func isLocalAddress(domain string) bool {
|
||||
return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.")
|
||||
}
|
||||
|
||||
// Ensures that HTTP or HTTPS is befor the adress if needed
|
||||
func addProtocol(domain string) string {
|
||||
if hasProtocol(domain) {
|
||||
return domain
|
||||
}
|
||||
if isLocalAddress(domain) {
|
||||
return "http://" + domain
|
||||
}
|
||||
return "https://" + domain
|
||||
}
|
||||
|
||||
func generateOpenSearchXML(config Config) {
|
||||
baseURL := addProtocol(config.Domain)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue