2018-08-12 22:24:49 +08:00
package rule
import (
"os"
"strings"
"github.com/nadoo/conflag"
)
2020-09-24 18:50:04 +08:00
// Config is config of rule.
2018-08-12 22:24:49 +08:00
type Config struct {
2021-12-22 21:20:29 +08:00
RulePath string
2018-08-12 22:24:49 +08:00
2020-11-20 18:11:25 +08:00
Forward [ ] string
Strategy Strategy
2018-08-12 22:24:49 +08:00
DNSServers [ ] string
IPSet string
Domain [ ] string
IP [ ] string
CIDR [ ] string
}
2020-11-20 18:11:25 +08:00
// Strategy configurations.
type Strategy struct {
2022-02-20 19:50:23 +08:00
Strategy string
Check string
CheckInterval int
CheckTimeout int
CheckTolerance int
CheckLatencySamples int
CheckDisabledOnly bool
MaxFailures int
DialTimeout int
RelayTimeout int
IntFace string
2020-09-24 18:50:04 +08:00
}
2020-08-16 12:00:46 +08:00
// NewConfFromFile returns a new config from file.
2018-08-12 22:24:49 +08:00
func NewConfFromFile ( ruleFile string ) ( * Config , error ) {
2021-12-22 21:20:29 +08:00
p := & Config { RulePath : ruleFile }
2018-08-12 22:24:49 +08:00
f := conflag . NewFromFile ( "rule" , ruleFile )
f . StringSliceUniqVar ( & p . Forward , "forward" , nil , "forward url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS[,SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS]" )
2020-11-20 18:11:25 +08:00
f . StringVar ( & p . Strategy . Strategy , "strategy" , "rr" , "forward strategy, default: rr" )
2020-11-21 01:20:40 +08:00
f . StringVar ( & p . Strategy . Check , "check" , "http://www.msftconnecttest.com/connecttest.txt#expect=200" , "check=tcp[://HOST:PORT]: tcp port connect check\ncheck=http://HOST[:PORT][/URI][#expect=STRING_IN_RESP_LINE]\ncheck=file://SCRIPT_PATH: run a check script, healthy when exitcode=0, environment variables: FORWARDER_ADDR\ncheck=disable: disable health check" )
2020-11-20 18:11:25 +08:00
f . IntVar ( & p . Strategy . CheckInterval , "checkinterval" , 30 , "fowarder check interval(seconds)" )
f . IntVar ( & p . Strategy . CheckTimeout , "checktimeout" , 10 , "fowarder check timeout(seconds)" )
2022-02-20 21:37:36 +08:00
f . IntVar ( & p . Strategy . CheckLatencySamples , "checklatencysamples" , 10 , "use the average latency of the latest N checks" )
2020-11-20 18:11:25 +08:00
f . IntVar ( & p . Strategy . CheckTolerance , "checktolerance" , 0 , "fowarder check tolerance(ms), switch only when new_latency < old_latency - tolerance, only used in lha mode" )
f . BoolVar ( & p . Strategy . CheckDisabledOnly , "checkdisabledonly" , false , "check disabled fowarders only" )
f . IntVar ( & p . Strategy . MaxFailures , "maxfailures" , 3 , "max failures to change forwarder status to disabled" )
f . IntVar ( & p . Strategy . DialTimeout , "dialtimeout" , 3 , "dial timeout(seconds)" )
f . IntVar ( & p . Strategy . RelayTimeout , "relaytimeout" , 0 , "relay timeout(seconds)" )
f . StringVar ( & p . Strategy . IntFace , "interface" , "" , "source ip or source interface" )
2018-08-12 22:24:49 +08:00
f . StringSliceUniqVar ( & p . DNSServers , "dnsserver" , nil , "remote dns server" )
2022-01-26 22:31:56 +08:00
f . StringVar ( & p . IPSet , "ipset" , "" , "ipset NAME, will create 2 sets: NAME for ipv4 and NAME6 for ipv6" )
2018-08-12 22:24:49 +08:00
2022-01-30 12:34:14 +08:00
f . StringSliceVar ( & p . Domain , "domain" , nil , "domain" )
f . StringSliceVar ( & p . IP , "ip" , nil , "ip" )
f . StringSliceVar ( & p . CIDR , "cidr" , nil , "cidr" )
2018-08-12 22:24:49 +08:00
err := f . Parse ( )
if err != nil {
return nil , err
}
return p , err
}
2020-08-16 12:00:46 +08:00
// ListDir returns file list named with suffix in dirPth.
2018-08-12 22:24:49 +08:00
func ListDir ( dirPth string , suffix string ) ( files [ ] string , err error ) {
files = make ( [ ] string , 0 , 10 )
2021-02-06 00:26:58 +08:00
dir , err := os . ReadDir ( dirPth )
2018-08-12 22:24:49 +08:00
if err != nil {
return nil , err
}
PthSep := string ( os . PathSeparator )
2020-08-16 12:00:46 +08:00
suffix = strings . ToLower ( suffix )
2018-08-12 22:24:49 +08:00
for _ , fi := range dir {
if fi . IsDir ( ) {
continue
}
2020-08-16 12:00:46 +08:00
if strings . HasSuffix ( strings . ToLower ( fi . Name ( ) ) , suffix ) {
2018-08-12 22:24:49 +08:00
files = append ( files , dirPth + PthSep + fi . Name ( ) )
}
}
return files , nil
}