diff --git a/config.go b/config.go index 4ce995f..c4bb092 100644 --- a/config.go +++ b/config.go @@ -17,18 +17,20 @@ var flag = conflag.New() type Config struct { Verbose bool - Listen []string + Listens []string - Forward []string + Forwards []string StrategyConfig rule.StrategyConfig - RuleFile []string - RulesDir string + RuleFiles []string + RulesDir string DNS string DNSConfig dns.Config rules []*rule.Config + + Services []string } func parseConfig() *Config { @@ -37,9 +39,9 @@ func parseConfig() *Config { flag.SetOutput(os.Stdout) flag.BoolVar(&conf.Verbose, "verbose", false, "verbose mode") - flag.StringSliceUniqVar(&conf.Listen, "listen", nil, "listen url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS") + flag.StringSliceUniqVar(&conf.Listens, "listen", nil, "listen url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS") - flag.StringSliceUniqVar(&conf.Forward, "forward", nil, "forward url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS[,SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS]") + flag.StringSliceUniqVar(&conf.Forwards, "forward", nil, "forward url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS[,SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS]") flag.StringVar(&conf.StrategyConfig.Strategy, "strategy", "rr", "forward strategy, default: rr") flag.StringVar(&conf.StrategyConfig.CheckWebSite, "checkwebsite", "www.apple.com", "proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80") flag.IntVar(&conf.StrategyConfig.CheckInterval, "checkinterval", 30, "proxy check interval(seconds)") @@ -50,7 +52,7 @@ func parseConfig() *Config { flag.IntVar(&conf.StrategyConfig.RelayTimeout, "relaytimeout", 0, "relay timeout(seconds)") flag.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface") - flag.StringSliceUniqVar(&conf.RuleFile, "rulefile", nil, "rule file path") + flag.StringSliceUniqVar(&conf.RuleFiles, "rulefile", nil, "rule file path") flag.StringVar(&conf.RulesDir, "rules-dir", "", "rule file folder") flag.StringVar(&conf.DNS, "dns", "", "local dns server listen address") @@ -61,6 +63,8 @@ func parseConfig() *Config { flag.IntVar(&conf.DNSConfig.MinTTL, "dnsminttl", 0, "minimum TTL value for entries in the CACHE(seconds)") flag.StringSliceUniqVar(&conf.DNSConfig.Records, "dnsrecord", nil, "custom dns record, format: domain/ip") + flag.StringSliceUniqVar(&conf.Services, "service", nil, "enable services") + flag.Usage = usage err := flag.Parse() if err != nil { @@ -74,14 +78,14 @@ func parseConfig() *Config { log.F = log.Debugf } - if len(conf.Listen) == 0 && conf.DNS == "" { + if len(conf.Listens) == 0 && conf.DNS == "" && len(conf.Services) == 0 { // flag.Usage() fmt.Fprintf(os.Stderr, "ERROR: listen url must be specified.\n") os.Exit(-1) } // rulefiles - for _, ruleFile := range conf.RuleFile { + for _, ruleFile := range conf.RuleFiles { if !path.IsAbs(ruleFile) { ruleFile = path.Join(flag.ConfDir(), ruleFile) } diff --git a/features.go b/feature.go similarity index 79% rename from features.go rename to feature.go index f33b931..99e0c7c 100644 --- a/features.go +++ b/feature.go @@ -1,7 +1,10 @@ package main import ( - // comment out the protocol you don't need to make the compiled binary smaller. + // comment out the services you don't need to make the compiled binary smaller. + // _ "github.com/nadoo/glider/service/xxx" + + // comment out the protocols you don't need to make the compiled binary smaller. _ "github.com/nadoo/glider/proxy/http" _ "github.com/nadoo/glider/proxy/kcp" _ "github.com/nadoo/glider/proxy/mixed" diff --git a/feature_linux.go b/feature_linux.go new file mode 100644 index 0000000..8c37b96 --- /dev/null +++ b/feature_linux.go @@ -0,0 +1,10 @@ +package main + +import ( + // comment out the services you don't need to make the compiled binary smaller. + // _ "github.com/nadoo/glider/service/xxx" + + // comment out the protocols you don't need to make the compiled binary smaller. + _ "github.com/nadoo/glider/proxy/redir" + _ "github.com/nadoo/glider/proxy/unix" +) diff --git a/features_linux.go b/features_linux.go deleted file mode 100644 index 80d02e3..0000000 --- a/features_linux.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import ( - // comment out the protocol you don't need to make the compiled binary smaller. - _ "github.com/nadoo/glider/proxy/redir" - _ "github.com/nadoo/glider/proxy/unix" -) diff --git a/main.go b/main.go index 8391b84..a2d2d43 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "net" "os" "os/signal" + "strings" "syscall" "time" @@ -13,6 +14,7 @@ import ( "github.com/nadoo/glider/ipset" "github.com/nadoo/glider/proxy" "github.com/nadoo/glider/rule" + "github.com/nadoo/glider/service" ) var ( @@ -22,7 +24,7 @@ var ( func main() { // global rule proxy - pxy := rule.NewProxy(config.Forward, &config.StrategyConfig, config.rules) + pxy := rule.NewProxy(config.Forwards, &config.StrategyConfig, config.rules) // ipset manager ipsetM, _ := ipset.NewManager(config.rules) @@ -64,8 +66,8 @@ func main() { // enable checkers pxy.Check() - // Proxy Servers - for _, listen := range config.Listen { + // run proxy servers + for _, listen := range config.Listens { local, err := proxy.ServerFromURL(listen, pxy) if err != nil { log.Fatal(err) @@ -74,6 +76,12 @@ func main() { go local.ListenAndServe() } + // run services + for _, s := range config.Services { + args := strings.Split(s, ",") + go service.Run(args[0], args[1:]...) + } + sigCh := make(chan os.Signal, 1) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) <-sigCh diff --git a/proxy/dialer.go b/proxy/dialer.go index be82bee..5710052 100644 --- a/proxy/dialer.go +++ b/proxy/dialer.go @@ -5,8 +5,6 @@ import ( "net" "net/url" "strings" - - "github.com/nadoo/glider/common/log" ) // Dialer is used to create connection. @@ -42,7 +40,7 @@ var ( // RegisterDialer is used to register a dialer. func RegisterDialer(name string, c DialerCreator) { - dialerCreators[name] = c + dialerCreators[strings.ToLower(name)] = c } // DialerFromURL calls the registered creator to create dialers. @@ -54,7 +52,6 @@ func DialerFromURL(s string, dialer Dialer) (Dialer, error) { u, err := url.Parse(s) if err != nil { - log.F("parse err: %s", err) return nil, err } diff --git a/proxy/server.go b/proxy/server.go index 2e1cc28..e749d5b 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -5,8 +5,6 @@ import ( "net" "net/url" "strings" - - "github.com/nadoo/glider/common/log" ) // Server interface @@ -27,7 +25,7 @@ var ( // RegisterServer is used to register a proxy server func RegisterServer(name string, c ServerCreator) { - serverCreators[name] = c + serverCreators[strings.ToLower(name)] = c } // ServerFromURL calls the registered creator to create proxy servers @@ -43,7 +41,6 @@ func ServerFromURL(s string, p Proxy) (Server, error) { u, err := url.Parse(s) if err != nil { - log.F("parse err: %s", err) return nil, err } diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..c8baf23 --- /dev/null +++ b/service/service.go @@ -0,0 +1,29 @@ +package service + +import ( + "strings" + + "github.com/nadoo/glider/common/log" +) + +// Service is a server that can be run. +type Service interface { + Run(args ...string) +} + +var services = make(map[string]Service) + +// Register is used to register a service. +func Register(name string, s Service) { + services[strings.ToLower(name)] = s +} + +// Run runs a service. +func Run(name string, args ...string) { + svc, ok := services[strings.ToLower(name)] + if !ok { + log.F("[service] unknown service name: %s", name) + return + } + svc.Run(args...) +}