@@ -2,6 +2,7 @@ package main
22
33import (
44 "archive/zip"
5+ "errors"
56 "fmt"
67 "io/ioutil"
78 "net"
@@ -23,43 +24,72 @@ type ip2proxyItem struct {
2324}
2425
2526type ip2proxy struct {
26- items []* ip2proxyItem
27- archive []* zip.File
28- OutputDir string
29- ErrorsChan chan Error
30- Token string
27+ items []* ip2proxyItem
28+ archive []* zip.File
29+ OutputDir string
30+ ErrorsChan chan Error
31+ Token string
32+ Filename string
33+ Name string
34+ csvFilename string
35+ zipFilename string
36+ PrintType bool
3137}
3238
33- func (o * ip2proxy ) Get () {
34- answer , err := o .download ()
39+ func (o * ip2proxy ) checkErr (err error , message string ) bool {
3540 if err != nil {
36- o .ErrorsChan <- Error {err , "ip2proxy" , "Download" }
41+ o .ErrorsChan <- Error {err , o .Name , message }
42+ return true
43+ }
44+ printMessage (o .Name , message , "OK" )
45+ return false
46+ }
47+
48+ func (o * ip2proxy ) Get () {
49+ if o .Name == "ip2proxyPro" {
50+ o .csvFilename = "IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP.CSV"
51+ o .zipFilename = "PX4-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP"
52+ } else if o .Name == "ip2proxyLite" {
53+ o .csvFilename = "IP2PROXY-LITE-PX4.CSV"
54+ o .zipFilename = "PX4LITE"
55+ } else {
56+ o .ErrorsChan <- Error {errors .New ("Unknown ip2proxy type requested" ), o .Name , "bad init" }
3757 return
3858 }
39- printMessage ("ip2proxy" , "Download" , "OK" )
40- if err := o .unpack (answer ); err != nil {
41- o .ErrorsChan <- Error {err , "ip2proxy" , "Unpack" }
59+ fileData , err := o .getZip ()
60+ if o .checkErr (err , "Get ZIP" ) {
4261 return
4362 }
44- printMessage ("ip2proxy" , "Unpack" , "OK" )
45- if err := o .Parse ("IP2PROXY-LITE-PX4.CSV" ); err != nil {
46- o .ErrorsChan <- Error {err , "ip2proxy" , "Parse" }
63+ err = o .unpack (fileData )
64+ if o .checkErr (err , "Unpack" ) {
4765 return
4866 }
49- printMessage ("ip2proxy" , "Parse" , "OK" )
50- if err := o .Write (); err != nil {
51- o .ErrorsChan <- Error {err , "ip2proxy" , "Write Nginx Map" }
67+ err = o .Parse (o .csvFilename )
68+ if o .checkErr (err , "Parse" ) {
69+ return
70+ }
71+ err = o .Write ()
72+ if o .checkErr (err , "Write Nginx Map" ) {
5273 return
5374 }
54- printMessage ("ip2proxy" , "Write Nginx Map" , "OK" )
5575 o .ErrorsChan <- Error {err : nil }
5676}
5777
78+ func (o * ip2proxy ) getZip () ([]byte , error ) {
79+ if len (o .Token ) > 0 {
80+ return o .download ()
81+ } else if len (o .Filename ) > 0 {
82+ return ioutil .ReadFile (o .Filename )
83+ } else {
84+ return nil , errors .New ("Token or Filename must be passed" )
85+ }
86+ }
87+
5888func (o * ip2proxy ) download () ([]byte , error ) {
5989 client := & http.Client {}
6090 req , err := http .NewRequest ("GET" , "https://www.ip2location.com/download" , nil )
6191 q := req .URL .Query ()
62- q .Add ("file" , "PX4LITE" )
92+ q .Add ("file" , o . zipFilename )
6393 q .Add ("token" , o .Token )
6494 req .URL .RawQuery = q .Encode ()
6595 resp , err := client .Do (req )
@@ -87,10 +117,10 @@ func (o *ip2proxy) unpack(response []byte) error {
87117
88118func (o * ip2proxy ) Parse (filename string ) error {
89119 var list []* ip2proxyItem
90- for record := range readCSVDatabase (o .archive , filename , "ip2proxy" , ',' , false ) {
120+ for record := range readCSVDatabase (o .archive , filename , o . Name , ',' , false ) {
91121 item , err := o .lineToItem (record )
92122 if err != nil {
93- printMessage ("ip2proxy" , fmt .Sprintf ("Can't parse line from %s with %v" , filename , err ), "WARN" )
123+ printMessage (o . Name , fmt .Sprintf ("Can't parse line from %s with %v" , filename , err ), "WARN" )
94124 continue
95125 }
96126 list = append (list , item )
@@ -130,13 +160,19 @@ func (o *ip2proxy) Write() error {
130160}
131161
132162func (o * ip2proxy ) writeNetworks () error {
133- file , err := os .Create (path .Join (o .OutputDir , "ip2proxy_net .txt" ))
163+ file , err := os .Create (path .Join (o .OutputDir , o . Name + "_net .txt" ))
134164 if err != nil {
135165 return err
136166 }
137167 defer file .Close ()
168+ var mapValue string
138169 for _ , item := range o .items {
139- fmt .Fprintf (file , "%s-%s 1;\n " , item .IPFrom , item .IPTo )
170+ if o .PrintType {
171+ mapValue = item .ProxyType
172+ } else {
173+ mapValue = "1"
174+ }
175+ fmt .Fprintf (file , "%s-%s \" %s\" ;\n " , item .IPFrom , item .IPTo , mapValue )
140176 }
141177 return nil
142178}
0 commit comments