2018-08-12 22:24:49 +08:00
package rule
import (
"io/ioutil"
"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 {
2019-09-19 18:03:48 +08:00
Name string
2018-08-12 22:24:49 +08:00
Forward [ ] string
2020-09-23 22:14:18 +08:00
StrategyConfig StrategyConfig
2018-08-12 22:24:49 +08:00
DNSServers [ ] string
IPSet string
Domain [ ] string
IP [ ] string
CIDR [ ] string
}
2020-09-24 18:50:04 +08:00
// StrategyConfig is config of strategy.
type StrategyConfig struct {
Strategy string
CheckWebSite string
CheckInterval int
CheckTimeout int
2020-10-12 19:07:54 +08:00
CheckTolerance int
2020-09-24 18:50:04 +08:00
CheckDisabledOnly bool
MaxFailures int
DialTimeout int
RelayTimeout int
IntFace string
}
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 ) {
2019-09-19 18:03:48 +08:00
p := & Config { Name : 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]" )
f . StringVar ( & p . StrategyConfig . Strategy , "strategy" , "rr" , "forward strategy, default: rr" )
2020-10-12 19:07:54 +08:00
f . StringVar ( & p . StrategyConfig . CheckWebSite , "checkwebsite" , "www.apple.com" , "fowarder check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80" )
f . IntVar ( & p . StrategyConfig . CheckInterval , "checkinterval" , 30 , "fowarder check interval(seconds)" )
f . IntVar ( & p . StrategyConfig . CheckTimeout , "checktimeout" , 10 , "fowarder check timeout(seconds)" )
f . IntVar ( & p . StrategyConfig . CheckTolerance , "checktolerance" , 0 , "fowarder check tolerance(ms), switch only when new_latency < old_latency - tolerance, only used in lha mode" )
2020-04-21 21:51:27 +08:00
f . BoolVar ( & p . StrategyConfig . CheckDisabledOnly , "checkdisabledonly" , false , "check disabled fowarders only" )
f . IntVar ( & p . StrategyConfig . MaxFailures , "maxfailures" , 3 , "max failures to change forwarder status to disabled" )
2020-05-04 16:51:41 +08:00
f . IntVar ( & p . StrategyConfig . DialTimeout , "dialtimeout" , 3 , "dial timeout(seconds)" )
f . IntVar ( & p . StrategyConfig . RelayTimeout , "relaytimeout" , 0 , "relay timeout(seconds)" )
2018-08-20 22:23:00 +08:00
f . StringVar ( & p . StrategyConfig . IntFace , "interface" , "" , "source ip or source interface" )
2018-08-12 22:24:49 +08:00
f . StringSliceUniqVar ( & p . DNSServers , "dnsserver" , nil , "remote dns server" )
f . StringVar ( & p . IPSet , "ipset" , "" , "ipset name" )
f . StringSliceUniqVar ( & p . Domain , "domain" , nil , "domain" )
f . StringSliceUniqVar ( & p . IP , "ip" , nil , "ip" )
f . StringSliceUniqVar ( & p . CIDR , "cidr" , nil , "cidr" )
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 )
dir , err := ioutil . ReadDir ( dirPth )
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
}