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"`
}
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)
if err != nil {
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
err = decoder.Decode(c)
if err != nil {
return
}

View File

@ -4,7 +4,7 @@ import (
"context"
"github.com/net-byte/vtun/common/config"
"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"
)

60
main.go
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,7 +3,7 @@ package xcrypto
import (
"github.com/golang/snappy"
"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{}

View File

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

View File

@ -9,7 +9,7 @@ import (
"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/xproto"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water"
"github.com/pion/dtls/v2"
"log"

View File

@ -3,7 +3,7 @@ package h1
import (
"context"
"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"
"log"
"time"

View File

@ -10,8 +10,8 @@ import (
"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/xproto"
"github.com/net-byte/vtun/common/xtun"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/vtun/common/x/xtun"
"github.com/net-byte/water"
"golang.org/x/net/http2"
"io"

View File

@ -8,7 +8,7 @@ import (
"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/xproto"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water"
"io"
"log"

View File

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

View File

@ -8,7 +8,7 @@ import (
"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/xproto"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water"
"github.com/xtaci/kcp-go"
"golang.org/x/crypto/pbkdf2"

View File

@ -3,17 +3,16 @@ package quic
import (
"context"
"crypto/tls"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/vtun/common/x/xtun"
"log"
"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/net-byte/vtun/common/cache"
"github.com/net-byte/vtun/common/cipher"
"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/water"
"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/counter"
"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/quic-go/quic-go"
"log"

View File

@ -4,14 +4,13 @@ import (
"context"
"errors"
"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"
"net"
"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/net-byte/vtun/common/cache"
"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/counter"
"github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/common/xcrypto"
"github.com/net-byte/vtun/common/xproto"
"github.com/net-byte/vtun/common/x/xcrypto"
"github.com/net-byte/vtun/common/x/xproto"
"github.com/net-byte/water"
"log"
"net"

View File

@ -7,7 +7,7 @@ import (
"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/xtun"
"github.com/net-byte/vtun/common/x/xtun"
"github.com/net-byte/vtun/transport/protocol/tcp"
"github.com/net-byte/water"
"log"

View File

@ -3,7 +3,7 @@ package utls
import (
"context"
"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"
utls "github.com/refraction-networking/utls"
"log"

View File

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