mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
add config file support.
This commit is contained in:
parent
e27e482272
commit
e4ce0edc39
3
.gitignore
vendored
3
.gitignore
vendored
@ -15,7 +15,6 @@
|
|||||||
|
|
||||||
# custom
|
# custom
|
||||||
*.zip
|
*.zip
|
||||||
|
*.conf
|
||||||
glider
|
glider
|
||||||
|
|
||||||
doc/
|
doc/
|
||||||
|
42
README.md
42
README.md
@ -7,18 +7,21 @@ glider is a forward proxy with several protocols support.
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
```bash
|
```bash
|
||||||
glider v0.1 usage:
|
glider v0.2 usage:
|
||||||
-checksite string
|
-checkduration int
|
||||||
proxy check address (default "www.apple.com:443")
|
|
||||||
-duration int
|
|
||||||
proxy check duration(seconds) (default 30)
|
proxy check duration(seconds) (default 30)
|
||||||
-f value
|
-checkhost string
|
||||||
|
proxy check address (default "www.apple.com:443")
|
||||||
|
-config string
|
||||||
|
config file path
|
||||||
|
-forward value
|
||||||
forward url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT[,SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT]
|
forward url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT[,SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT]
|
||||||
-l value
|
-listen value
|
||||||
listen url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT
|
listen url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT
|
||||||
-s string
|
-strategy string
|
||||||
forward strategy, default: rr (default "rr")
|
forward strategy, default: rr (default "rr")
|
||||||
-v verbose mode
|
-verbose
|
||||||
|
verbose mode
|
||||||
|
|
||||||
Available Schemas:
|
Available Schemas:
|
||||||
mixed: serve as a http/socks5 proxy on the same port. (default)
|
mixed: serve as a http/socks5 proxy on the same port. (default)
|
||||||
@ -41,31 +44,34 @@ Available forward strategies:
|
|||||||
ha: High Availability mode
|
ha: High Availability mode
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
glider -l :8443 -v
|
glider -config glider.conf
|
||||||
|
-run glider with specified config file.
|
||||||
|
|
||||||
|
glider -listen :8443
|
||||||
-listen on :8443, serve as http/socks5 proxy on the same port.
|
-listen on :8443, serve as http/socks5 proxy on the same port.
|
||||||
|
|
||||||
glider -l ss://AEAD_CHACHA20_POLY1305:pass@:8443
|
glider -listen ss://AEAD_CHACHA20_POLY1305:pass@:8443
|
||||||
-listen on 0.0.0.0:8443 as a ss server.
|
-listen on 0.0.0.0:8443 as a shadowsocks server.
|
||||||
|
|
||||||
glider -l socks5://:1080 -v
|
glider -listen socks5://:1080 -verbose
|
||||||
-listen on :1080 as a socks5 proxy server, in verbose mode.
|
-listen on :1080 as a socks5 proxy server, in verbose mode.
|
||||||
|
|
||||||
glider -l http://:8080 -f socks5://127.0.0.1:1080 -v
|
glider -listen http://:8080 -forward socks5://127.0.0.1:1080
|
||||||
-listen on :8080 as a http proxy server, forward all requests via socks5 server.
|
-listen on :8080 as a http proxy server, forward all requests via socks5 server.
|
||||||
|
|
||||||
glider -l redir://:1081 -f ss://method:pass@1.1.1.1:443
|
glider -listen redir://:1081 -forward ss://method:pass@1.1.1.1:443
|
||||||
-listen on :1081 as a transparent redirect server, forward all requests via remote ss server.
|
-listen on :1081 as a transparent redirect server, forward all requests via remote ss server.
|
||||||
|
|
||||||
glider -l tcptun://:80=2.2.2.2:80 -f ss://method:pass@1.1.1.1:443
|
glider -listen tcptun://:80=2.2.2.2:80 -forward ss://method:pass@1.1.1.1:443
|
||||||
-listen on :80 and forward all requests to 2.2.2.2:80 via remote ss server.
|
-listen on :80 and forward all requests to 2.2.2.2:80 via remote ss server.
|
||||||
|
|
||||||
glider -l socks5://:1080 -l http://:8080 -f ss://method:pass@1.1.1.1:443
|
glider -listen socks5://:1080 -listen http://:8080 -forward ss://method:pass@1.1.1.1:443
|
||||||
-listen on :1080 as socks5 server, :8080 as http proxy server, forward all requests via remote ss server.
|
-listen on :1080 as socks5 server, :8080 as http proxy server, forward all requests via remote ss server.
|
||||||
|
|
||||||
glider -l redir://:1081 -l dnstun://:53=8.8.8.8:53 -f ss://method:pass@server1:port1,ss://method:pass@server2:port2
|
glider -listen redir://:1081 -listen dnstun://:53=8.8.8.8:53 -forward ss://method:pass@server1:port1,ss://method:pass@server2:port2
|
||||||
-listen on :1081 as transparent redirect server, :53 as dns server, use forward chain: server1 -> server2.
|
-listen on :1081 as transparent redirect server, :53 as dns server, use forward chain: server1 -> server2.
|
||||||
|
|
||||||
glider -l socks5://:1080 -f ss://method:pass@server1:port1 -f ss://method:pass@server2:port2 -s rr
|
glider -listen socks5://:1080 -forward ss://method:pass@server1:port1 -forward ss://method:pass@server2:port2 -strategy rr
|
||||||
-listen on :1080 as socks5 server, forward requests via server1 and server2 in roundrbin mode.
|
-listen on :1080 as socks5 server, forward requests via server1 and server2 in roundrbin mode.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
31
glider.conf.sample
Normal file
31
glider.conf.sample
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
### glider config file
|
||||||
|
|
||||||
|
# verbose mode, print logs
|
||||||
|
verbose
|
||||||
|
|
||||||
|
# listen on 8443, serve as http/socks5 proxy on the same port.
|
||||||
|
listen=:8443
|
||||||
|
|
||||||
|
# listen on 8443 as a ss server.
|
||||||
|
# listen=ss://AEAD_CHACHA20_POLY1305:pass@:8443
|
||||||
|
|
||||||
|
# listen=http://:8080
|
||||||
|
# listen=socks5://:1080
|
||||||
|
# listen=redir://:1081
|
||||||
|
|
||||||
|
# listen on udp port 53, forward dns requests via tcp protocol
|
||||||
|
listen=dnstun://:53=8.8.8.8:53
|
||||||
|
|
||||||
|
# upstream forward proxy
|
||||||
|
forward=socks5://192.168.1.10:1080
|
||||||
|
|
||||||
|
# upstream forward proxy
|
||||||
|
forward=ss://method:pass@1.1.1.1:443
|
||||||
|
|
||||||
|
# multiple upstream proxies forwad strategy
|
||||||
|
strategy=rr
|
||||||
|
|
||||||
|
# check address (to check a whether a forward proxy)
|
||||||
|
checkhost=www.apple.com:443
|
||||||
|
# check duration
|
||||||
|
checkduration=30
|
70
main.go
70
main.go
@ -1,33 +1,40 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/nadoo/conflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "0.1.3"
|
// VERSION .
|
||||||
|
const VERSION = "0.2"
|
||||||
|
|
||||||
var config struct {
|
var conf struct {
|
||||||
Verbose bool
|
Verbose bool
|
||||||
Strategy string
|
Strategy string
|
||||||
CheckSite string
|
CheckHost string
|
||||||
CheckDuration int
|
CheckDuration int
|
||||||
|
Listen arrFlags
|
||||||
|
Forward arrFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var flag = conflag.New()
|
||||||
|
|
||||||
func logf(f string, v ...interface{}) {
|
func logf(f string, v ...interface{}) {
|
||||||
if config.Verbose {
|
if conf.Verbose {
|
||||||
log.Printf(f, v...)
|
log.Printf(f, v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
app := os.Args[0]
|
app := os.Args[0]
|
||||||
fmt.Fprintf(os.Stderr, "%s v%s usage:\n", app, version)
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
fmt.Fprintf(os.Stderr, "%s v%s usage:\n", app, VERSION)
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
|
||||||
@ -57,31 +64,34 @@ func usage() {
|
|||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "Examples:\n")
|
fmt.Fprintf(os.Stderr, "Examples:\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l :8443 -v\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -config glider.conf\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " -run glider with specified config file.\n")
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
fmt.Fprintf(os.Stderr, " "+app+" -listen :8443\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :8443, serve as http/socks5 proxy on the same port.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :8443, serve as http/socks5 proxy on the same port.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l ss://AEAD_CHACHA20_POLY1305:pass@:8443\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen ss://AEAD_CHACHA20_POLY1305:pass@:8443\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on 0.0.0.0:8443 as a shadowsocks server.\n")
|
fmt.Fprintf(os.Stderr, " -listen on 0.0.0.0:8443 as a shadowsocks server.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l socks5://:1080 -v\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen socks5://:1080 -verbose\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :1080 as a socks5 proxy server, in verbose mode.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :1080 as a socks5 proxy server, in verbose mode.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l http://:8080 -f socks5://127.0.0.1:1080 -v\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen http://:8080 -forward socks5://127.0.0.1:1080\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :8080 as a http proxy server, forward all requests via socks5 server.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :8080 as a http proxy server, forward all requests via socks5 server.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l redir://:1081 -f ss://method:pass@1.1.1.1:443\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen redir://:1081 -forward ss://method:pass@1.1.1.1:443\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :1081 as a transparent redirect server, forward all requests via remote ss server.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :1081 as a transparent redirect server, forward all requests via remote ss server.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l tcptun://:80=2.2.2.2:80 -f ss://method:pass@1.1.1.1:443\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen tcptun://:80=2.2.2.2:80 -forward ss://method:pass@1.1.1.1:443\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :80 and forward all requests to 2.2.2.2:80 via remote ss server.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :80 and forward all requests to 2.2.2.2:80 via remote ss server.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l socks5://:1080 -l http://:8080 -f ss://method:pass@1.1.1.1:443\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen socks5://:1080 -listen http://:8080 -forward ss://method:pass@1.1.1.1:443\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :1080 as socks5 server, :8080 as http proxy server, forward all requests via remote ss server.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :1080 as socks5 server, :8080 as http proxy server, forward all requests via remote ss server.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l redir://:1081 -l dnstun://:53=8.8.8.8:53 -f ss://method:pass@server1:port1,ss://method:pass@server2:port2\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen redir://:1081 -listen dnstun://:53=8.8.8.8:53 -forward ss://method:pass@server1:port1,ss://method:pass@server2:port2\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :1081 as transparent redirect server, :53 as dns server, use forward chain: server1 -> server2.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :1081 as transparent redirect server, :53 as dns server, use forward chain: server1 -> server2.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Fprintf(os.Stderr, " "+app+" -l socks5://:1080 -f ss://method:pass@server1:port1 -f ss://method:pass@server2:port2 -s rr\n")
|
fmt.Fprintf(os.Stderr, " "+app+" -listen socks5://:1080 -forward ss://method:pass@server1:port1 -forward ss://method:pass@server2:port2 -strategy rr\n")
|
||||||
fmt.Fprintf(os.Stderr, " -listen on :1080 as socks5 server, forward requests via server1 and server2 in roundrbin mode.\n")
|
fmt.Fprintf(os.Stderr, " -listen on :1080 as socks5 server, forward requests via server1 and server2 in roundrbin mode.\n")
|
||||||
fmt.Fprintf(os.Stderr, "\n")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
}
|
}
|
||||||
@ -101,30 +111,28 @@ func (i *arrFlags) Set(value string) error {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var flags struct {
|
flag.BoolVar(&conf.Verbose, "verbose", false, "verbose mode")
|
||||||
Listen arrFlags
|
flag.StringVar(&conf.Strategy, "strategy", "rr", "forward strategy, default: rr")
|
||||||
Forward arrFlags
|
flag.StringVar(&conf.CheckHost, "checkhost", "www.apple.com:443", "proxy check address")
|
||||||
}
|
flag.IntVar(&conf.CheckDuration, "checkduration", 30, "proxy check duration(seconds)")
|
||||||
|
flag.Var(&conf.Listen, "listen", "listen url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT")
|
||||||
flag.BoolVar(&config.Verbose, "v", false, "verbose mode")
|
flag.Var(&conf.Forward, "forward", "forward url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT[,SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT]")
|
||||||
flag.StringVar(&config.Strategy, "s", "rr", "forward strategy, default: rr")
|
|
||||||
flag.StringVar(&config.CheckSite, "checksite", "www.apple.com:443", "proxy check address")
|
|
||||||
flag.IntVar(&config.CheckDuration, "duration", 30, "proxy check duration(seconds)")
|
|
||||||
|
|
||||||
flag.Var(&flags.Listen, "l", "listen url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT")
|
|
||||||
flag.Var(&flags.Forward, "f", "forward url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT[,SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT]")
|
|
||||||
|
|
||||||
flag.Usage = usage
|
flag.Usage = usage
|
||||||
flag.Parse()
|
err := flag.Parse()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if len(flags.Listen) == 0 {
|
if len(conf.Listen) == 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")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var forwarders []Proxy
|
var forwarders []Proxy
|
||||||
for _, chain := range flags.Forward {
|
for _, chain := range conf.Forward {
|
||||||
var forward Proxy
|
var forward Proxy
|
||||||
var err error
|
var err error
|
||||||
for _, url := range strings.Split(chain, ",") {
|
for _, url := range strings.Split(chain, ",") {
|
||||||
@ -136,7 +144,7 @@ func main() {
|
|||||||
forwarders = append(forwarders, forward)
|
forwarders = append(forwarders, forward)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, listen := range flags.Listen {
|
for _, listen := range conf.Listen {
|
||||||
local, err := ProxyFromURL(listen, forwarders...)
|
local, err := ProxyFromURL(listen, forwarders...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
8
proxy.go
8
proxy.go
@ -93,7 +93,7 @@ func ProxyFromURL(s string, forwarders ...Proxy) (Proxy, error) {
|
|||||||
} else if len(forwarders) == 1 {
|
} else if len(forwarders) == 1 {
|
||||||
proxy = newProxy(addr, forwarders[0])
|
proxy = newProxy(addr, forwarders[0])
|
||||||
} else if len(forwarders) > 1 {
|
} else if len(forwarders) > 1 {
|
||||||
switch config.Strategy {
|
switch conf.Strategy {
|
||||||
case "rr":
|
case "rr":
|
||||||
proxy = newRRProxy(addr, forwarders)
|
proxy = newRRProxy(addr, forwarders)
|
||||||
logf("forward to remote servers in round robin mode.")
|
logf("forward to remote servers in round robin mode.")
|
||||||
@ -101,7 +101,7 @@ func ProxyFromURL(s string, forwarders ...Proxy) (Proxy, error) {
|
|||||||
proxy = newHAProxy(addr, forwarders)
|
proxy = newHAProxy(addr, forwarders)
|
||||||
logf("forward to remote servers in high availability mode.")
|
logf("forward to remote servers in high availability mode.")
|
||||||
default:
|
default:
|
||||||
logf("not supported forward mode '%s', just use the first forward server.", config.Strategy)
|
logf("not supported forward mode '%s', just use the first forward server.", conf.Strategy)
|
||||||
proxy = newProxy(addr, forwarders[0])
|
proxy = newProxy(addr, forwarders[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ func check(p Proxy, target string, duration int) {
|
|||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
c, err := p.Dial("tcp", target)
|
c, err := p.Dial("tcp", target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-check %s -> %s, set to DISABLED. error: %s", p.Addr(), config.CheckSite, err)
|
logf("proxy-check %s -> %s, set to DISABLED. error: %s", p.Addr(), conf.CheckHost, err)
|
||||||
p.SetEnable(false)
|
p.SetEnable(false)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -150,6 +150,6 @@ func check(p Proxy, target string, duration int) {
|
|||||||
|
|
||||||
// TODO: choose the fastest proxy.
|
// TODO: choose the fastest proxy.
|
||||||
dialTime := time.Since(startTime)
|
dialTime := time.Since(startTime)
|
||||||
logf("proxy-check: %s -> %s, connect time: %s", p.Addr(), config.CheckSite, dialTime.String())
|
logf("proxy-check: %s -> %s, connect time: %s", p.Addr(), conf.CheckHost, dialTime.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func newStrategyProxy(addr string, forwarders []Proxy) Proxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, forward := range forwarders {
|
for _, forward := range forwarders {
|
||||||
go check(forward, config.CheckSite, config.CheckDuration)
|
go check(forward, conf.CheckHost, conf.CheckDuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &strategyProxy{addr: addr, forwarders: forwarders}
|
return &strategyProxy{addr: addr, forwarders: forwarders}
|
||||||
|
Loading…
Reference in New Issue
Block a user