mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 01:15:41 +08:00
rules: add rules-dir folder config support
This commit is contained in:
parent
b2b3a94798
commit
989d3c9787
3
.gitignore
vendored
3
.gitignore
vendored
@ -17,5 +17,8 @@
|
||||
*.zip
|
||||
/*.conf
|
||||
/*.rule
|
||||
|
||||
rules.d/*.rule
|
||||
|
||||
glider
|
||||
bak/
|
||||
|
@ -10,8 +10,8 @@ type direct struct {
|
||||
var Direct = &direct{}
|
||||
|
||||
func (d *direct) Addr() string { return "127.0.0.1" }
|
||||
func (d *direct) ListenAndServe() { logf("base proxy ListenAndServe") }
|
||||
func (d *direct) Serve(c net.Conn) { logf("base proxy Serve") }
|
||||
func (d *direct) ListenAndServe() { logf("direct proxy ListenAndServe") }
|
||||
func (d *direct) Serve(c net.Conn) { logf("direct proxy Serve") }
|
||||
func (d *direct) CurrentProxy() Proxy { return d }
|
||||
func (d *direct) GetProxy(dstAddr string) Proxy { return d }
|
||||
func (d *direct) NextProxy() Proxy { return d }
|
||||
|
5
dns.go
5
dns.go
@ -24,6 +24,7 @@ const TCPDNSHEADERLen = 2 + UDPDNSHeaderLen
|
||||
// so we should also serve tcp requests.
|
||||
const MaxUDPDNSLen = 512
|
||||
|
||||
// DNS .
|
||||
type DNS struct {
|
||||
*proxy
|
||||
dnsServer string
|
||||
@ -31,7 +32,7 @@ type DNS struct {
|
||||
dnsServerMap map[string]string
|
||||
}
|
||||
|
||||
// DNSForwarder returns a dns forwarder. client -> dns.udp -> glider -> forwarder -> remote dns addr
|
||||
// NewDNS returns a dns forwarder. client -> dns.udp -> glider -> forwarder -> remote dns addr
|
||||
func NewDNS(addr, raddr string, upProxy Proxy) (*DNS, error) {
|
||||
s := &DNS{
|
||||
proxy: NewProxy(addr, upProxy),
|
||||
@ -70,7 +71,7 @@ func (s *DNS) ListenAndServe() {
|
||||
|
||||
dnsServer := s.GetServer(domain)
|
||||
// TODO: check here
|
||||
rc, err := s.GetProxy(domain+":53").GetProxy(domain+":53").Dial("tcp", dnsServer)
|
||||
rc, err := s.GetProxy(domain+":53").Dial("tcp", dnsServer)
|
||||
if err != nil {
|
||||
logf("failed to connect to server %v: %v", dnsServer, err)
|
||||
return
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
package main
|
||||
|
||||
// DNSTun .
|
||||
type DNSTun struct {
|
||||
*proxy
|
||||
raddr string
|
||||
|
4
http.go
4
http.go
@ -16,12 +16,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// httpproxy
|
||||
// HTTPProxy .
|
||||
type HTTPProxy struct {
|
||||
*proxy
|
||||
}
|
||||
|
||||
// HTTPProxy returns a http proxy.
|
||||
// NewHTTPProxy returns a http proxy.
|
||||
func NewHTTPProxy(addr string, upProxy Proxy) (*HTTPProxy, error) {
|
||||
s := &HTTPProxy{
|
||||
proxy: NewProxy(addr, upProxy),
|
||||
|
19
main.go
19
main.go
@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
// VERSION .
|
||||
const VERSION = "0.3.2"
|
||||
const VERSION = "0.4.0"
|
||||
|
||||
var conf struct {
|
||||
Verbose bool
|
||||
@ -22,6 +22,7 @@ var conf struct {
|
||||
Listen []string
|
||||
Forward []string
|
||||
RuleFile []string
|
||||
RulesDir string
|
||||
|
||||
DNS string
|
||||
DNSServer []string
|
||||
@ -122,6 +123,7 @@ func main() {
|
||||
flag.StringSliceUniqVar(&conf.Listen, "listen", nil, "listen url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT")
|
||||
flag.StringSliceUniqVar(&conf.Forward, "forward", nil, "forward url, format: SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT[,SCHEMA://[USER|METHOD:PASSWORD@][HOST]:PORT]")
|
||||
flag.StringSliceUniqVar(&conf.RuleFile, "rulefile", nil, "rule file path")
|
||||
flag.StringVar(&conf.RulesDir, "rules-dir", "rules.d", "rule file folder")
|
||||
|
||||
flag.StringVar(&conf.DNS, "dns", "", "dns listen address")
|
||||
flag.StringSliceUniqVar(&conf.DNSServer, "dnsserver", []string{"8.8.8.8:53"}, "remote dns server")
|
||||
@ -155,13 +157,24 @@ func main() {
|
||||
forwarders = append(forwarders, forward)
|
||||
}
|
||||
|
||||
// combine forwarders to a singer strategy forwarder
|
||||
// combine forwarders to a single strategy forwarder
|
||||
forwarder := NewStrategyForwarder(conf.Strategy, forwarders)
|
||||
|
||||
// rule forwarders
|
||||
var ruleForwarders []*RuleForwarder
|
||||
for _, ruleFile := range conf.RuleFile {
|
||||
ruleForwarder, err := NewRuleProxyFromFile(ruleFile)
|
||||
ruleForwarder, err := NewRuleForwarderFromFile(ruleFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ruleForwarders = append(ruleForwarders, ruleForwarder)
|
||||
}
|
||||
|
||||
// rules folder
|
||||
ruleFolderFiles, _ := listDir(conf.RulesDir, ".rule")
|
||||
for _, ruleFile := range ruleFolderFiles {
|
||||
ruleForwarder, err := NewRuleForwarderFromFile(ruleFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
6
mixed.go
6
mixed.go
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// https://www.ietf.org/rfc/rfc2616.txt, http methods must be uppercase.
|
||||
var httpMethods = [][]byte{
|
||||
var httpMethods = [...][]byte{
|
||||
[]byte("GET"),
|
||||
[]byte("POST"),
|
||||
[]byte("PUT"),
|
||||
@ -25,7 +25,7 @@ type MixedProxy struct {
|
||||
ss Proxy
|
||||
}
|
||||
|
||||
// MixedProxy returns a mixed proxy.
|
||||
// NewMixedProxy returns a mixed proxy.
|
||||
func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy, error) {
|
||||
p := &MixedProxy{
|
||||
proxy: NewProxy(addr, upProxy),
|
||||
@ -41,7 +41,7 @@ func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// mixedproxy .
|
||||
// ListenAndServe .
|
||||
func (p *MixedProxy) ListenAndServe() {
|
||||
l, err := net.Listen("tcp", p.addr)
|
||||
if err != nil {
|
||||
|
@ -4,6 +4,7 @@ package main
|
||||
|
||||
import "log"
|
||||
|
||||
// RedirProxy .
|
||||
type RedirProxy struct{ *proxy }
|
||||
|
||||
// NewRedirProxy returns a redirect proxy.
|
||||
|
6
rule.go
6
rule.go
@ -9,7 +9,7 @@ import (
|
||||
"github.com/nadoo/conflag"
|
||||
)
|
||||
|
||||
// RuleForwarder, every ruleForwarder points to a rule file
|
||||
// RuleForwarder , every ruleForwarder points to a rule file
|
||||
type RuleForwarder struct {
|
||||
Forward []string
|
||||
Strategy string
|
||||
@ -27,8 +27,8 @@ type RuleForwarder struct {
|
||||
Proxy
|
||||
}
|
||||
|
||||
// NewRuleProxyFromFile .
|
||||
func NewRuleProxyFromFile(ruleFile string) (*RuleForwarder, error) {
|
||||
// NewRuleForwarderFromFile .
|
||||
func NewRuleForwarderFromFile(ruleFile string) (*RuleForwarder, error) {
|
||||
p := &RuleForwarder{name: ruleFile}
|
||||
|
||||
f := conflag.NewFromFile("rule", ruleFile)
|
||||
|
1
rules.go
1
rules.go
@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// RulesForwarder .
|
||||
type RulesForwarder struct {
|
||||
globalForwarder Proxy
|
||||
|
||||
|
@ -55,6 +55,7 @@ var socks5Errors = []string{
|
||||
"address type not supported",
|
||||
}
|
||||
|
||||
// SOCKS5Proxy .
|
||||
type SOCKS5Proxy struct {
|
||||
*proxy
|
||||
network string
|
||||
@ -95,6 +96,7 @@ func (s *SOCKS5Proxy) ListenAndServe() {
|
||||
}
|
||||
}
|
||||
|
||||
// Serve .
|
||||
func (s *SOCKS5Proxy) Serve(c net.Conn) {
|
||||
defer c.Close()
|
||||
|
||||
|
3
ss.go
3
ss.go
@ -9,7 +9,7 @@ import (
|
||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
// ss
|
||||
// SSProxy .
|
||||
type SSProxy struct {
|
||||
*proxy
|
||||
core.StreamConnCipher
|
||||
@ -50,6 +50,7 @@ func (s *SSProxy) ListenAndServe() {
|
||||
}
|
||||
}
|
||||
|
||||
// Serve .
|
||||
func (s *SSProxy) Serve(c net.Conn) {
|
||||
defer c.Close()
|
||||
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import "net"
|
||||
|
||||
// TCPTun .
|
||||
type TCPTun struct {
|
||||
*proxy
|
||||
raddr string
|
||||
|
26
utils.go
Normal file
26
utils.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func listDir(dirPth string, suffix string) (files []string, err error) {
|
||||
files = make([]string, 0, 10)
|
||||
dir, err := ioutil.ReadDir(dirPth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
PthSep := string(os.PathSeparator)
|
||||
suffix = strings.ToUpper(suffix)
|
||||
for _, fi := range dir {
|
||||
if fi.IsDir() {
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
|
||||
files = append(files, dirPth+PthSep+fi.Name())
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user