mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +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
|
*.zip
|
||||||
/*.conf
|
/*.conf
|
||||||
/*.rule
|
/*.rule
|
||||||
|
|
||||||
|
rules.d/*.rule
|
||||||
|
|
||||||
glider
|
glider
|
||||||
bak/
|
bak/
|
||||||
|
@ -10,8 +10,8 @@ type direct struct {
|
|||||||
var Direct = &direct{}
|
var Direct = &direct{}
|
||||||
|
|
||||||
func (d *direct) Addr() string { return "127.0.0.1" }
|
func (d *direct) Addr() string { return "127.0.0.1" }
|
||||||
func (d *direct) ListenAndServe() { logf("base proxy ListenAndServe") }
|
func (d *direct) ListenAndServe() { logf("direct proxy ListenAndServe") }
|
||||||
func (d *direct) Serve(c net.Conn) { logf("base proxy Serve") }
|
func (d *direct) Serve(c net.Conn) { logf("direct proxy Serve") }
|
||||||
func (d *direct) CurrentProxy() Proxy { return d }
|
func (d *direct) CurrentProxy() Proxy { return d }
|
||||||
func (d *direct) GetProxy(dstAddr string) Proxy { return d }
|
func (d *direct) GetProxy(dstAddr string) Proxy { return d }
|
||||||
func (d *direct) NextProxy() 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.
|
// so we should also serve tcp requests.
|
||||||
const MaxUDPDNSLen = 512
|
const MaxUDPDNSLen = 512
|
||||||
|
|
||||||
|
// DNS .
|
||||||
type DNS struct {
|
type DNS struct {
|
||||||
*proxy
|
*proxy
|
||||||
dnsServer string
|
dnsServer string
|
||||||
@ -31,7 +32,7 @@ type DNS struct {
|
|||||||
dnsServerMap map[string]string
|
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) {
|
func NewDNS(addr, raddr string, upProxy Proxy) (*DNS, error) {
|
||||||
s := &DNS{
|
s := &DNS{
|
||||||
proxy: NewProxy(addr, upProxy),
|
proxy: NewProxy(addr, upProxy),
|
||||||
@ -70,7 +71,7 @@ func (s *DNS) ListenAndServe() {
|
|||||||
|
|
||||||
dnsServer := s.GetServer(domain)
|
dnsServer := s.GetServer(domain)
|
||||||
// TODO: check here
|
// 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 {
|
if err != nil {
|
||||||
logf("failed to connect to server %v: %v", dnsServer, err)
|
logf("failed to connect to server %v: %v", dnsServer, err)
|
||||||
return
|
return
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
// DNSTun .
|
||||||
type DNSTun struct {
|
type DNSTun struct {
|
||||||
*proxy
|
*proxy
|
||||||
raddr string
|
raddr string
|
||||||
|
4
http.go
4
http.go
@ -16,12 +16,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// httpproxy
|
// HTTPProxy .
|
||||||
type HTTPProxy struct {
|
type HTTPProxy struct {
|
||||||
*proxy
|
*proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPProxy returns a http proxy.
|
// NewHTTPProxy returns a http proxy.
|
||||||
func NewHTTPProxy(addr string, upProxy Proxy) (*HTTPProxy, error) {
|
func NewHTTPProxy(addr string, upProxy Proxy) (*HTTPProxy, error) {
|
||||||
s := &HTTPProxy{
|
s := &HTTPProxy{
|
||||||
proxy: NewProxy(addr, upProxy),
|
proxy: NewProxy(addr, upProxy),
|
||||||
|
19
main.go
19
main.go
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// VERSION .
|
// VERSION .
|
||||||
const VERSION = "0.3.2"
|
const VERSION = "0.4.0"
|
||||||
|
|
||||||
var conf struct {
|
var conf struct {
|
||||||
Verbose bool
|
Verbose bool
|
||||||
@ -22,6 +22,7 @@ var conf struct {
|
|||||||
Listen []string
|
Listen []string
|
||||||
Forward []string
|
Forward []string
|
||||||
RuleFile []string
|
RuleFile []string
|
||||||
|
RulesDir string
|
||||||
|
|
||||||
DNS string
|
DNS string
|
||||||
DNSServer []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.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.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.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.StringVar(&conf.DNS, "dns", "", "dns listen address")
|
||||||
flag.StringSliceUniqVar(&conf.DNSServer, "dnsserver", []string{"8.8.8.8:53"}, "remote dns server")
|
flag.StringSliceUniqVar(&conf.DNSServer, "dnsserver", []string{"8.8.8.8:53"}, "remote dns server")
|
||||||
@ -155,13 +157,24 @@ func main() {
|
|||||||
forwarders = append(forwarders, forward)
|
forwarders = append(forwarders, forward)
|
||||||
}
|
}
|
||||||
|
|
||||||
// combine forwarders to a singer strategy forwarder
|
// combine forwarders to a single strategy forwarder
|
||||||
forwarder := NewStrategyForwarder(conf.Strategy, forwarders)
|
forwarder := NewStrategyForwarder(conf.Strategy, forwarders)
|
||||||
|
|
||||||
// rule forwarders
|
// rule forwarders
|
||||||
var ruleForwarders []*RuleForwarder
|
var ruleForwarders []*RuleForwarder
|
||||||
for _, ruleFile := range conf.RuleFile {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
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.
|
// https://www.ietf.org/rfc/rfc2616.txt, http methods must be uppercase.
|
||||||
var httpMethods = [][]byte{
|
var httpMethods = [...][]byte{
|
||||||
[]byte("GET"),
|
[]byte("GET"),
|
||||||
[]byte("POST"),
|
[]byte("POST"),
|
||||||
[]byte("PUT"),
|
[]byte("PUT"),
|
||||||
@ -25,7 +25,7 @@ type MixedProxy struct {
|
|||||||
ss Proxy
|
ss Proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
// MixedProxy returns a mixed proxy.
|
// NewMixedProxy returns a mixed proxy.
|
||||||
func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy, error) {
|
func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy, error) {
|
||||||
p := &MixedProxy{
|
p := &MixedProxy{
|
||||||
proxy: NewProxy(addr, upProxy),
|
proxy: NewProxy(addr, upProxy),
|
||||||
@ -41,7 +41,7 @@ func NewMixedProxy(network, addr, user, pass string, upProxy Proxy) (*MixedProxy
|
|||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// mixedproxy .
|
// ListenAndServe .
|
||||||
func (p *MixedProxy) ListenAndServe() {
|
func (p *MixedProxy) ListenAndServe() {
|
||||||
l, err := net.Listen("tcp", p.addr)
|
l, err := net.Listen("tcp", p.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -4,6 +4,7 @@ package main
|
|||||||
|
|
||||||
import "log"
|
import "log"
|
||||||
|
|
||||||
|
// RedirProxy .
|
||||||
type RedirProxy struct{ *proxy }
|
type RedirProxy struct{ *proxy }
|
||||||
|
|
||||||
// NewRedirProxy returns a redirect proxy.
|
// NewRedirProxy returns a redirect proxy.
|
||||||
|
6
rule.go
6
rule.go
@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/nadoo/conflag"
|
"github.com/nadoo/conflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RuleForwarder, every ruleForwarder points to a rule file
|
// RuleForwarder , every ruleForwarder points to a rule file
|
||||||
type RuleForwarder struct {
|
type RuleForwarder struct {
|
||||||
Forward []string
|
Forward []string
|
||||||
Strategy string
|
Strategy string
|
||||||
@ -27,8 +27,8 @@ type RuleForwarder struct {
|
|||||||
Proxy
|
Proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRuleProxyFromFile .
|
// NewRuleForwarderFromFile .
|
||||||
func NewRuleProxyFromFile(ruleFile string) (*RuleForwarder, error) {
|
func NewRuleForwarderFromFile(ruleFile string) (*RuleForwarder, error) {
|
||||||
p := &RuleForwarder{name: ruleFile}
|
p := &RuleForwarder{name: ruleFile}
|
||||||
|
|
||||||
f := conflag.NewFromFile("rule", ruleFile)
|
f := conflag.NewFromFile("rule", ruleFile)
|
||||||
|
1
rules.go
1
rules.go
@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RulesForwarder .
|
||||||
type RulesForwarder struct {
|
type RulesForwarder struct {
|
||||||
globalForwarder Proxy
|
globalForwarder Proxy
|
||||||
|
|
||||||
|
@ -55,6 +55,7 @@ var socks5Errors = []string{
|
|||||||
"address type not supported",
|
"address type not supported",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SOCKS5Proxy .
|
||||||
type SOCKS5Proxy struct {
|
type SOCKS5Proxy struct {
|
||||||
*proxy
|
*proxy
|
||||||
network string
|
network string
|
||||||
@ -95,6 +96,7 @@ func (s *SOCKS5Proxy) ListenAndServe() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serve .
|
||||||
func (s *SOCKS5Proxy) Serve(c net.Conn) {
|
func (s *SOCKS5Proxy) Serve(c net.Conn) {
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
3
ss.go
3
ss.go
@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ss
|
// SSProxy .
|
||||||
type SSProxy struct {
|
type SSProxy struct {
|
||||||
*proxy
|
*proxy
|
||||||
core.StreamConnCipher
|
core.StreamConnCipher
|
||||||
@ -50,6 +50,7 @@ func (s *SSProxy) ListenAndServe() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serve .
|
||||||
func (s *SSProxy) Serve(c net.Conn) {
|
func (s *SSProxy) Serve(c net.Conn) {
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import "net"
|
import "net"
|
||||||
|
|
||||||
|
// TCPTun .
|
||||||
type TCPTun struct {
|
type TCPTun struct {
|
||||||
*proxy
|
*proxy
|
||||||
raddr string
|
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