mirror of
				https://github.com/nadoo/glider.git
				synced 2025-11-04 07:42:38 +08:00 
			
		
		
		
	service: add service module
This commit is contained in:
		
							parent
							
								
									9ca06ee32f
								
							
						
					
					
						commit
						4e966cc319
					
				
							
								
								
									
										22
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								config.go
									
									
									
									
									
								
							@ -17,18 +17,20 @@ var flag = conflag.New()
 | 
				
			|||||||
type Config struct {
 | 
					type Config struct {
 | 
				
			||||||
	Verbose bool
 | 
						Verbose bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Listen []string
 | 
						Listens []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Forward        []string
 | 
						Forwards       []string
 | 
				
			||||||
	StrategyConfig rule.StrategyConfig
 | 
						StrategyConfig rule.StrategyConfig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	RuleFile []string
 | 
						RuleFiles []string
 | 
				
			||||||
	RulesDir string
 | 
						RulesDir  string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	DNS       string
 | 
						DNS       string
 | 
				
			||||||
	DNSConfig dns.Config
 | 
						DNSConfig dns.Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rules []*rule.Config
 | 
						rules []*rule.Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Services []string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseConfig() *Config {
 | 
					func parseConfig() *Config {
 | 
				
			||||||
@ -37,9 +39,9 @@ func parseConfig() *Config {
 | 
				
			|||||||
	flag.SetOutput(os.Stdout)
 | 
						flag.SetOutput(os.Stdout)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flag.BoolVar(&conf.Verbose, "verbose", false, "verbose mode")
 | 
						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.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.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)")
 | 
						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.IntVar(&conf.StrategyConfig.RelayTimeout, "relaytimeout", 0, "relay timeout(seconds)")
 | 
				
			||||||
	flag.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface")
 | 
						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.RulesDir, "rules-dir", "", "rule file folder")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flag.StringVar(&conf.DNS, "dns", "", "local dns server listen address")
 | 
						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.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.DNSConfig.Records, "dnsrecord", nil, "custom dns record, format: domain/ip")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						flag.StringSliceUniqVar(&conf.Services, "service", nil, "enable services")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flag.Usage = usage
 | 
						flag.Usage = usage
 | 
				
			||||||
	err := flag.Parse()
 | 
						err := flag.Parse()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@ -74,14 +78,14 @@ func parseConfig() *Config {
 | 
				
			|||||||
		log.F = log.Debugf
 | 
							log.F = log.Debugf
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(conf.Listen) == 0 && conf.DNS == "" {
 | 
						if len(conf.Listens) == 0 && conf.DNS == "" && len(conf.Services) == 0 {
 | 
				
			||||||
		// flag.Usage()
 | 
							// flag.Usage()
 | 
				
			||||||
		fmt.Fprintf(os.Stderr, "ERROR: listen url must be specified.\n")
 | 
							fmt.Fprintf(os.Stderr, "ERROR: listen url must be specified.\n")
 | 
				
			||||||
		os.Exit(-1)
 | 
							os.Exit(-1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// rulefiles
 | 
						// rulefiles
 | 
				
			||||||
	for _, ruleFile := range conf.RuleFile {
 | 
						for _, ruleFile := range conf.RuleFiles {
 | 
				
			||||||
		if !path.IsAbs(ruleFile) {
 | 
							if !path.IsAbs(ruleFile) {
 | 
				
			||||||
			ruleFile = path.Join(flag.ConfDir(), ruleFile)
 | 
								ruleFile = path.Join(flag.ConfDir(), ruleFile)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,10 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					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/http"
 | 
				
			||||||
	_ "github.com/nadoo/glider/proxy/kcp"
 | 
						_ "github.com/nadoo/glider/proxy/kcp"
 | 
				
			||||||
	_ "github.com/nadoo/glider/proxy/mixed"
 | 
						_ "github.com/nadoo/glider/proxy/mixed"
 | 
				
			||||||
							
								
								
									
										10
									
								
								feature_linux.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								feature_linux.go
									
									
									
									
									
										Normal file
									
								
							@ -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"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
@ -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"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
							
								
								
									
										14
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								main.go
									
									
									
									
									
								
							@ -5,6 +5,7 @@ import (
 | 
				
			|||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/signal"
 | 
						"os/signal"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"syscall"
 | 
						"syscall"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -13,6 +14,7 @@ import (
 | 
				
			|||||||
	"github.com/nadoo/glider/ipset"
 | 
						"github.com/nadoo/glider/ipset"
 | 
				
			||||||
	"github.com/nadoo/glider/proxy"
 | 
						"github.com/nadoo/glider/proxy"
 | 
				
			||||||
	"github.com/nadoo/glider/rule"
 | 
						"github.com/nadoo/glider/rule"
 | 
				
			||||||
 | 
						"github.com/nadoo/glider/service"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
@ -22,7 +24,7 @@ var (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	// global rule proxy
 | 
						// global rule proxy
 | 
				
			||||||
	pxy := rule.NewProxy(config.Forward, &config.StrategyConfig, config.rules)
 | 
						pxy := rule.NewProxy(config.Forwards, &config.StrategyConfig, config.rules)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// ipset manager
 | 
						// ipset manager
 | 
				
			||||||
	ipsetM, _ := ipset.NewManager(config.rules)
 | 
						ipsetM, _ := ipset.NewManager(config.rules)
 | 
				
			||||||
@ -64,8 +66,8 @@ func main() {
 | 
				
			|||||||
	// enable checkers
 | 
						// enable checkers
 | 
				
			||||||
	pxy.Check()
 | 
						pxy.Check()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Proxy Servers
 | 
						// run proxy servers
 | 
				
			||||||
	for _, listen := range config.Listen {
 | 
						for _, listen := range config.Listens {
 | 
				
			||||||
		local, err := proxy.ServerFromURL(listen, pxy)
 | 
							local, err := proxy.ServerFromURL(listen, pxy)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Fatal(err)
 | 
								log.Fatal(err)
 | 
				
			||||||
@ -74,6 +76,12 @@ func main() {
 | 
				
			|||||||
		go local.ListenAndServe()
 | 
							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)
 | 
						sigCh := make(chan os.Signal, 1)
 | 
				
			||||||
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
 | 
						signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
 | 
				
			||||||
	<-sigCh
 | 
						<-sigCh
 | 
				
			||||||
 | 
				
			|||||||
@ -5,8 +5,6 @@ import (
 | 
				
			|||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/nadoo/glider/common/log"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Dialer is used to create connection.
 | 
					// Dialer is used to create connection.
 | 
				
			||||||
@ -42,7 +40,7 @@ var (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// RegisterDialer is used to register a dialer.
 | 
					// RegisterDialer is used to register a dialer.
 | 
				
			||||||
func RegisterDialer(name string, c DialerCreator) {
 | 
					func RegisterDialer(name string, c DialerCreator) {
 | 
				
			||||||
	dialerCreators[name] = c
 | 
						dialerCreators[strings.ToLower(name)] = c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DialerFromURL calls the registered creator to create dialers.
 | 
					// 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)
 | 
						u, err := url.Parse(s)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.F("parse err: %s", err)
 | 
					 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -5,8 +5,6 @@ import (
 | 
				
			|||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/nadoo/glider/common/log"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Server interface
 | 
					// Server interface
 | 
				
			||||||
@ -27,7 +25,7 @@ var (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// RegisterServer is used to register a proxy server
 | 
					// RegisterServer is used to register a proxy server
 | 
				
			||||||
func RegisterServer(name string, c ServerCreator) {
 | 
					func RegisterServer(name string, c ServerCreator) {
 | 
				
			||||||
	serverCreators[name] = c
 | 
						serverCreators[strings.ToLower(name)] = c
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ServerFromURL calls the registered creator to create proxy servers
 | 
					// 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)
 | 
						u, err := url.Parse(s)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.F("parse err: %s", err)
 | 
					 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										29
									
								
								service/service.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								service/service.go
									
									
									
									
									
										Normal file
									
								
							@ -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...)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user