@@ -2,30 +2,30 @@ package main
22
33import (
44 "bufio"
5+ "bytes"
56 "flag"
7+ "fmt"
8+ "io"
69 "net/http"
710 "os"
811 "path/filepath"
912 "runtime"
10- "io"
11- "bytes"
12- "fmt"
13- "strings"
13+ "strings"
1414)
1515
1616var (
17- trackerUrl = "https://cdn.jsdelivr.net/gh/ngosang/trackerslist/trackers_best_ip.txt"
18- //trackerUrl = "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt" //need http proxy
17+ trackerUrl = "https://cdn.jsdelivr.net/gh/ngosang/trackerslist/trackers_best_ip.txt"
18+ //trackerUrl = "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt" //need http proxy
1919)
2020
2121func getTrackers () ([]string , error ) {
22- client := & http.Client {}
23- request , err := http .NewRequest ("GET" , trackerUrl , nil )
24- if err != nil {
25- return nil , err
26- }
27- //add header
28- request .Header .Add ("User-Agent" ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" )
22+ client := & http.Client {}
23+ request , err := http .NewRequest ("GET" , trackerUrl , nil )
24+ if err != nil {
25+ return nil , err
26+ }
27+ //add header
28+ request .Header .Add ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" )
2929
3030 resp , err := client .Do (request )
3131 if err != nil {
@@ -47,90 +47,114 @@ func getTrackers() ([]string, error) {
4747}
4848
4949func getConfFilePath () (string , error ) {
50- var (
51- home string
52- confPath string
53- err error
54- )
50+ var (
51+ home string
52+ confPath string
53+ err error
54+ )
5555 home , err = os .UserHomeDir ()
5656 if err != nil {
5757 return "" , err
5858 }
5959 confPath = filepath .Join (home , "aria2" , "aria2.conf" )
60- return confPath , nil
60+ return confPath , nil
61+ }
62+
63+ func readConfLines (confPath string ) ([]string , error ) {
64+ var (
65+ lines []string
66+ line string
67+ line_bs []byte
68+ file * os.File
69+ reader * bufio.Reader
70+ err error
71+ )
72+ file , err = os .OpenFile (confPath , os .O_RDONLY , 0666 )
73+ reader = bufio .NewReader (file )
74+ if err != nil {
75+ fmt .Printf ("open config file[%s] error!%v\n " , confPath , err )
76+ return nil , err
77+ }
78+ for {
79+ line_bs , _ , err = reader .ReadLine ()
80+ if err != nil {
81+ if err == io .EOF {
82+ break
83+ } else {
84+ fmt .Printf ("read config file[%s] error!%v\n " , confPath , err )
85+ return nil , err
86+ }
87+ }
88+ line = string (line_bs )
89+ if strings .HasPrefix (line , "#" ) || strings .Index (line , "=" ) > 0 {
90+ lines = append (lines , line )
91+ }
92+ }
93+ return lines , nil
6194}
6295
63- func updateConfFile (confPath string , trackers []string ) error {
64- var (
65- err error
66- file * os.File
67- lineb []byte
68- line string
69- reader * bufio.Reader
70- bufw * bytes.Buffer
71- )
96+ func updateConfFile (confPath string , lines []string , trackers []string ) error {
97+ var (
98+ err error
99+ file * os.File
100+ line string
101+ bufw * bytes.Buffer
102+ )
72103
73- file , err = os .OpenFile (confPath , os .O_RDWR , 0666 )
74- if err != nil {
75- fmt .Println ( "Open conf file error!" , err )
76- return err
77- }
78- defer file .Close ()
104+ file , err = os .OpenFile (confPath , os .O_WRONLY | os . O_TRUNC , 0666 )
105+ if err != nil {
106+ fmt .Printf ( "open config file[%s] error!%v \n " , confPath , err )
107+ return err
108+ }
109+ defer file .Close ()
79110
80- reader = bufio .NewReader (file )
81- bufw = new (bytes.Buffer )
82- for {
83- lineb , _ , err = reader .ReadLine ()
84- if err != nil {
85- if err == io .EOF {
86- break
87- } else {
88- fmt .Println ("Read file error!" , err )
89- return err
90- }
91- }
92- line = string (lineb )
93- if strings .HasPrefix (line , "bt-tracker" ) &&
94- ! strings .HasPrefix (line , "bt-tracker-" ) {
95- line = "bt-tracker=" + strings .Join (trackers , "," )
96- }
97- bufw .WriteString (line )
98- if "windows" == runtime .GOOS {
99- bufw .WriteString ("\r \n " )
100- }else {
101- bufw .WriteString ("\n " )
111+ bufw = new (bytes.Buffer )
112+ for _ , line = range lines {
113+ if strings .HasPrefix (line , "bt-tracker" ) &&
114+ ! strings .HasPrefix (line , "bt-tracker-" ) {
115+ line = "bt-tracker=" + strings .Join (trackers , "," )
116+ }
117+ bufw .WriteString (line )
118+ if "windows" == runtime .GOOS {
119+ bufw .WriteString ("\r \n " )
120+ } else {
121+ bufw .WriteString ("\n " )
122+ }
102123 }
103- }
104- file .Seek (0 , 0 )
105- _ , err = file .WriteString (bufw .String ())
106- if err != nil {
107- fmt .Println ("Write conf file error!" , err )
108- return err
109- }
110- return nil
124+ _ , err = file .WriteString (bufw .String ())
125+ if err != nil {
126+ fmt .Printf ("write conf file[%s] error!%v\n " , confPath , err )
127+ return err
128+ }
129+ return nil
111130}
112131
113132func main () {
114133 flag .Parse ()
115134 if len (flag .Arg (0 )) > 0 {
116135 trackerUrl = flag .Arg (0 )
117136 }
118- var (
119- trackers []string
120- err error
121- confPath string
122- )
137+ var (
138+ trackers []string
139+ confLines []string
140+ err error
141+ confPath string
142+ )
123143 trackers , err = getTrackers ()
124144 if err != nil {
125145 panic (err .Error ())
126146 }
127- confPath , err = getConfFilePath ()
147+ confPath , err = getConfFilePath ()
148+ if err != nil {
149+ panic (err .Error ())
150+ }
151+ confLines , err = readConfLines (confPath )
128152 if err != nil {
129153 panic (err .Error ())
130154 }
131- err = updateConfFile (confPath , trackers )
155+ err = updateConfFile (confPath , confLines , trackers )
132156 if err != nil {
133157 panic (err .Error ())
134158 }
135- fmt .Println ("Update tracker success!" )
159+ fmt .Println ("Update tracker success!" )
136160}
0 commit comments