support for loading default settings from configuration files

This commit is contained in:
NNdroid 2023-08-25 19:05:43 +08:00
parent db41279373
commit d962570016
34 changed files with 101 additions and 65 deletions

View File

@ -35,14 +35,48 @@ type Config struct {
Host string `json:"host"` Host string `json:"host"`
} }
func (c Config) LoadConfig(configFile string, config *Config) (err error) { type nativeConfig Config
var DefaultConfig = nativeConfig{
DeviceName: "",
LocalAddr: ":3000",
ServerAddr: ":3001",
ServerIP: "172.16.0.1",
ServerIPv6: "fced:9999::1",
CIDR: "172.16.0.10/24",
CIDRv6: "fced:9999::9999/64",
Key: "freedom@2023",
Protocol: "udp",
Path: "/freedom",
ServerMode: false,
GlobalMode: false,
Obfs: false,
Compress: false,
MTU: 1500,
Timeout: 30,
TLSCertificateFilePath: "./certs/server.pem",
TLSCertificateKeyFilePath: "./certs/server.key",
TLSSni: "",
TLSInsecureSkipVerify: false,
Verbose: false,
PSKMode: false,
Host: "",
}
func (c *Config) UnmarshalJSON(data []byte) error {
_ = json.Unmarshal(data, &DefaultConfig)
*c = Config(DefaultConfig)
return nil
}
func (c *Config) LoadConfig(configFile string) (err error) {
file, err := os.Open(configFile) file, err := os.Open(configFile)
if err != nil { if err != nil {
return return
} }
defer file.Close() defer file.Close()
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
err = decoder.Decode(config) err = decoder.Decode(c)
if err != nil { if err != nil {
return return
} }

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water" "github.com/net-byte/water"
) )

60
main.go
View File

@ -25,42 +25,46 @@ func displayVersionInfo() {
log.Printf("go version -> %s", _goVersion) log.Printf("go version -> %s", _goVersion)
} }
func main() { var cfg = config.Config{}
config := config.Config{} var configFile string
var configFile string
func init() {
flag.StringVar(&configFile, "f", "", "config file") flag.StringVar(&configFile, "f", "", "config file")
flag.StringVar(&config.DeviceName, "dn", "", "device name") flag.StringVar(&cfg.DeviceName, "dn", config.DefaultConfig.DeviceName, "device name")
flag.StringVar(&config.CIDR, "c", "172.16.0.10/24", "tun interface cidr") flag.StringVar(&cfg.CIDR, "c", config.DefaultConfig.CIDR, "tun interface cidr")
flag.StringVar(&config.CIDRv6, "c6", "fced:9999::9999/64", "tun interface ipv6 cidr") flag.StringVar(&cfg.CIDRv6, "c6", config.DefaultConfig.CIDRv6, "tun interface ipv6 cidr")
flag.IntVar(&config.MTU, "mtu", 1500, "tun mtu") flag.IntVar(&cfg.MTU, "mtu", config.DefaultConfig.MTU, "tun mtu")
flag.StringVar(&config.LocalAddr, "l", ":3000", "local address") flag.StringVar(&cfg.LocalAddr, "l", config.DefaultConfig.LocalAddr, "local address")
flag.StringVar(&config.ServerAddr, "s", ":3001", "server address") flag.StringVar(&cfg.ServerAddr, "s", config.DefaultConfig.ServerAddr, "server address")
flag.StringVar(&config.ServerIP, "sip", "172.16.0.1", "server ip") flag.StringVar(&cfg.ServerIP, "sip", config.DefaultConfig.ServerIP, "server ip")
flag.StringVar(&config.ServerIPv6, "sip6", "fced:9999::1", "server ipv6") flag.StringVar(&cfg.ServerIPv6, "sip6", config.DefaultConfig.ServerIPv6, "server ipv6")
flag.StringVar(&config.Key, "k", "freedom@2023", "key") flag.StringVar(&cfg.Key, "k", config.DefaultConfig.Key, "key")
flag.StringVar(&config.Protocol, "p", "udp", "protocol udp/tls/grpc/quic/utls/dtls/h2/http/tcp/https/ws/wss") flag.StringVar(&cfg.Protocol, "p", config.DefaultConfig.Protocol, "protocol udp/tls/grpc/quic/utls/dtls/h2/http/tcp/https/ws/wss")
flag.StringVar(&config.Path, "path", "/freedom", "path") flag.StringVar(&cfg.Path, "path", config.DefaultConfig.Path, "path")
flag.BoolVar(&config.ServerMode, "S", false, "server mode") flag.BoolVar(&cfg.ServerMode, "S", config.DefaultConfig.ServerMode, "server mode")
flag.BoolVar(&config.GlobalMode, "g", false, "client global mode") flag.BoolVar(&cfg.GlobalMode, "g", config.DefaultConfig.GlobalMode, "client global mode")
flag.BoolVar(&config.Obfs, "obfs", false, "enable data obfuscation") flag.BoolVar(&cfg.Obfs, "obfs", config.DefaultConfig.Obfs, "enable data obfuscation")
flag.BoolVar(&config.Compress, "compress", false, "enable data compression") flag.BoolVar(&cfg.Compress, "compress", config.DefaultConfig.Compress, "enable data compression")
flag.IntVar(&config.Timeout, "t", 30, "dial timeout in seconds") flag.IntVar(&cfg.Timeout, "t", config.DefaultConfig.Timeout, "dial timeout in seconds")
flag.StringVar(&config.TLSCertificateFilePath, "certificate", "./certs/server.pem", "tls certificate file path") flag.StringVar(&cfg.TLSCertificateFilePath, "certificate", config.DefaultConfig.TLSCertificateFilePath, "tls certificate file path")
flag.StringVar(&config.TLSCertificateKeyFilePath, "privatekey", "./certs/server.key", "tls certificate key file path") flag.StringVar(&cfg.TLSCertificateKeyFilePath, "privatekey", config.DefaultConfig.TLSCertificateKeyFilePath, "tls certificate key file path")
flag.StringVar(&config.TLSSni, "sni", "", "tls handshake sni") flag.StringVar(&cfg.TLSSni, "sni", config.DefaultConfig.TLSSni, "tls handshake sni")
flag.BoolVar(&config.TLSInsecureSkipVerify, "isv", false, "tls insecure skip verify") flag.BoolVar(&cfg.TLSInsecureSkipVerify, "isv", config.DefaultConfig.TLSInsecureSkipVerify, "tls insecure skip verify")
flag.BoolVar(&config.Verbose, "v", false, "enable verbose output") flag.BoolVar(&cfg.Verbose, "v", config.DefaultConfig.Verbose, "enable verbose output")
flag.BoolVar(&config.PSKMode, "psk", false, "enable psk mode (dtls only)") flag.BoolVar(&cfg.PSKMode, "psk", config.DefaultConfig.PSKMode, "enable psk mode (dtls only)")
flag.StringVar(&config.Host, "host", "", "http host") flag.StringVar(&cfg.Host, "host", config.DefaultConfig.Host, "http host")
flag.Parse() flag.Parse()
}
func main() {
displayVersionInfo() displayVersionInfo()
if configFile != "" { if configFile != "" {
err := config.LoadConfig(configFile, &config) err := cfg.LoadConfig(configFile)
if err != nil { if err != nil {
log.Fatalf("Failed to load config from file: %s", err) log.Fatalf("Failed to load config from file: %s", err)
} }
} }
app := app.NewApp(&config, _version) app := app.NewApp(&cfg, _version)
app.InitConfig() app.InitConfig()
go app.StartApp() go app.StartApp()
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)

View File

@ -2,7 +2,7 @@ package dtlsclient
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/dtls" "github.com/net-byte/vtun/transport/protocol/dtls"
) )

View File

@ -2,7 +2,7 @@ package h1client
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/h1" "github.com/net-byte/vtun/transport/protocol/h1"
) )

View File

@ -2,7 +2,7 @@ package h2client
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/h2" "github.com/net-byte/vtun/transport/protocol/h2"
) )

View File

@ -2,7 +2,7 @@ package kcpclient
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/kcp" "github.com/net-byte/vtun/transport/protocol/kcp"
) )

View File

@ -2,7 +2,7 @@ package quicclient
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/quic" "github.com/net-byte/vtun/transport/protocol/quic"
) )

View File

@ -2,7 +2,7 @@ package tcpclient
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/tcp" "github.com/net-byte/vtun/transport/protocol/tcp"
) )

View File

@ -2,7 +2,7 @@ package tlsclient
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/tls" "github.com/net-byte/vtun/transport/protocol/tls"
) )

View File

@ -2,7 +2,7 @@ package utlsclient
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/utls" "github.com/net-byte/vtun/transport/protocol/utls"
) )

View File

@ -2,7 +2,7 @@ package wsclient
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xchan" "github.com/net-byte/vtun/common/x/xchan"
kc "github.com/net-byte/vtun/mobile/config" kc "github.com/net-byte/vtun/mobile/config"
"github.com/net-byte/vtun/transport/protocol/ws" "github.com/net-byte/vtun/transport/protocol/ws"
) )

View File

@ -3,7 +3,7 @@ package xcrypto
import ( import (
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/net-byte/vtun/common/cipher" "github.com/net-byte/vtun/common/cipher"
"github.com/net-byte/vtun/common/xcrypto" "github.com/net-byte/vtun/common/x/xcrypto"
) )
var xp = xcrypto.XCrypto{} var xp = xcrypto.XCrypto{}

View File

@ -5,8 +5,8 @@ import (
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/net-byte/vtun/common/cipher" "github.com/net-byte/vtun/common/cipher"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/vtun/common/xtun" "github.com/net-byte/vtun/common/x/xtun"
"github.com/pion/dtls/v2" "github.com/pion/dtls/v2"
"log" "log"
"net" "net"

View File

@ -9,7 +9,7 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water" "github.com/net-byte/water"
"github.com/pion/dtls/v2" "github.com/pion/dtls/v2"
"log" "log"

View File

@ -3,7 +3,7 @@ package h1
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/xtun" "github.com/net-byte/vtun/common/x/xtun"
"github.com/net-byte/vtun/transport/protocol/tcp" "github.com/net-byte/vtun/transport/protocol/tcp"
"log" "log"
"time" "time"

View File

@ -10,8 +10,8 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/vtun/common/xtun" "github.com/net-byte/vtun/common/x/xtun"
"github.com/net-byte/water" "github.com/net-byte/water"
"golang.org/x/net/http2" "golang.org/x/net/http2"
"io" "io"

View File

@ -8,7 +8,7 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water" "github.com/net-byte/water"
"io" "io"
"log" "log"

View File

@ -4,8 +4,8 @@ import (
"context" "context"
"crypto/sha1" "crypto/sha1"
"errors" "errors"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/vtun/common/xtun" "github.com/net-byte/vtun/common/x/xtun"
"log" "log"
"runtime" "runtime"
"strings" "strings"

View File

@ -8,7 +8,7 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water" "github.com/net-byte/water"
"github.com/xtaci/kcp-go" "github.com/xtaci/kcp-go"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"

View File

@ -3,17 +3,16 @@ package quic
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/vtun/common/x/xtun"
"log" "log"
"time" "time"
"github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/xproto"
"github.com/net-byte/vtun/common/xtun"
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/net-byte/vtun/common/cache" "github.com/net-byte/vtun/common/cache"
"github.com/net-byte/vtun/common/cipher" "github.com/net-byte/vtun/common/cipher"
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/water" "github.com/net-byte/water"
"github.com/quic-go/quic-go" "github.com/quic-go/quic-go"

View File

@ -9,7 +9,7 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water" "github.com/net-byte/water"
"github.com/quic-go/quic-go" "github.com/quic-go/quic-go"
"log" "log"

View File

@ -4,14 +4,13 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/net-byte/vtun/common/x/xcrypto"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/vtun/common/x/xtun"
"log" "log"
"net" "net"
"time" "time"
"github.com/net-byte/vtun/common/xcrypto"
"github.com/net-byte/vtun/common/xproto"
"github.com/net-byte/vtun/common/xtun"
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/net-byte/vtun/common/cache" "github.com/net-byte/vtun/common/cache"
"github.com/net-byte/vtun/common/cipher" "github.com/net-byte/vtun/common/cipher"

View File

@ -9,8 +9,8 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xcrypto" "github.com/net-byte/vtun/common/x/xcrypto"
"github.com/net-byte/vtun/common/xproto" "github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water" "github.com/net-byte/water"
"log" "log"
"net" "net"

View File

@ -7,7 +7,7 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xtun" "github.com/net-byte/vtun/common/x/xtun"
"github.com/net-byte/vtun/transport/protocol/tcp" "github.com/net-byte/vtun/transport/protocol/tcp"
"github.com/net-byte/water" "github.com/net-byte/water"
"log" "log"

View File

@ -3,7 +3,7 @@ package utls
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/xtun" "github.com/net-byte/vtun/common/x/xtun"
"github.com/net-byte/vtun/transport/protocol/tcp" "github.com/net-byte/vtun/transport/protocol/tcp"
utls "github.com/refraction-networking/utls" utls "github.com/refraction-networking/utls"
"log" "log"

View File

@ -2,7 +2,7 @@ package ws
import ( import (
"context" "context"
"github.com/net-byte/vtun/common/xtun" "github.com/net-byte/vtun/common/x/xtun"
"log" "log"
"net" "net"
"time" "time"