mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 01:15:41 +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 {
|
||||
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)
|
||||
}
|
||||
|
@ -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"
|
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"
|
||||
"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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
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