// overrideConfig should contain options from command line to override options // in config file. func parseConfig(rc string, override *Config) { // fmt.Println("rcFile:", path) f, err := os.Open(expandTilde(rc)) if err != nil { if os.IsNotExist(err) { fmt.Printf("Config file %s not found, using default options\n", rc) } else { fmt.Println("Error opening config file:", err) } return } defer f.Close() IgnoreUTF8BOM(f) scanner := bufio.NewScanner(f) parser := reflect.ValueOf(configParser{}) zeroMethod := reflect.Value{} var lines []string // store lines for upgrade var n int for scanner.Scan() { lines = append(lines, scanner.Text()) n++ line := strings.TrimSpace(scanner.Text()) if line == "" || line[0] == '#' { continue } v := strings.Split(line, "=") if len(v) != 2 { Fatal("config syntax error on line", n) } key, val := strings.TrimSpace(v[0]), strings.TrimSpace(v[1]) methodName := "Parse" + strings.ToUpper(key[0:1]) + key[1:] method := parser.MethodByName(methodName) if method == zeroMethod { Fatalf("no such option \"%s\"\n", key) } // for backward compatibility, allow empty string in shadowMethod and logFile if val == "" && key != "shadowMethod" && key != "logFile" { Fatalf("empty %s, please comment or remove unused option\n", key) } args := []reflect.Value{reflect.ValueOf(val)} method.Call(args) } if scanner.Err() != nil { Fatalf("Error reading rc file: %v\n", scanner.Err()) } overrideConfig(&config, override) checkConfig() if configNeedUpgrade { upgradeConfig(rc, lines) } }
func initDomainList(domainListFile string, domainType DomainType) { var err error if err = isFileExists(domainListFile); err != nil { return } f, err := os.Open(domainListFile) if err != nil { errl.Println("Error opening domain list:", err) return } defer f.Close() directList.Lock() defer directList.Unlock() scanner := bufio.NewScanner(f) for scanner.Scan() { domain := strings.TrimSpace(scanner.Text()) if domain == "" { continue } debug.Printf("Loaded domain %s as type %v", domain, domainType) directList.Domain[domain] = domainType } if scanner.Err() != nil { errl.Printf("Error reading domain list %s: %v\n", domainListFile, scanner.Err()) } }
func loadSiteList(fpath string) (lst []string, err error) { var exists bool if exists, err = isFileExists(fpath); err != nil { errl.Printf("Error loading domaint list: %v\n", err) } if !exists { return } f, err := os.Open(fpath) if err != nil { errl.Println("Error opening domain list:", err) return } defer f.Close() scanner := bufio.NewScanner(f) lst = make([]string, 0) for scanner.Scan() { site := strings.TrimSpace(scanner.Text()) if site == "" { continue } lst = append(lst, site) } if scanner.Err() != nil { errl.Printf("Error reading domain list %s: %v\n", fpath, scanner.Err()) } return lst, scanner.Err() }
func initDirectList() { var exists bool var err error if exists, err = isFileExists(configPath.alwaysDirect); err != nil { errl.Printf("Error loading direct domain list: %v\n", err) } if !exists { return } f, err := os.Open(configPath.alwaysDirect) if err != nil { errl.Println("Error opening domain list:", err) return } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { domain := strings.TrimSpace(scanner.Text()) if domain == "" { continue } directList.Domain[domain] = domainTypeDirect } if scanner.Err() != nil { errl.Printf("Error reading domain list %s: %v\n", configPath.alwaysDirect, scanner.Err()) } }
func loadUserPasswdFile(file string) { if file == "" { return } f, err := os.Open(file) if err != nil { Fatal("error opening user passwd fle:", err) } r := bufio.NewReader(f) s := bufio.NewScanner(r) for s.Scan() { addUserPasswd(s.Text()) } f.Close() }