Skip to content

Commit 3df89b6

Browse files
authored
Merge pull request #22 from m-messiah/issue-21
Generate ip2proxy Pro base from local file
2 parents a6f751f + 3d9fb49 commit 3df89b6

File tree

4 files changed

+87
-28
lines changed

4 files changed

+87
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ output
44
*.exe
55
.vscode
66
output/
7+
.DS_Store

app.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ func main() {
1111
outputDir := flag.String("output", "output", "output directory for files")
1212
ipgeobase := flag.Bool("ipgeobase", false, "enable ipgeobase generation")
1313
tor := flag.Bool("tor", false, "enable tor generation")
14-
ip2proxyFlag := flag.Bool("ip2proxy", false, "enable ip2proxy generation")
15-
ip2proxyToken := flag.String("ip2proxy-token", "", "Get token here https://lite.ip2location.com/file-download")
14+
ip2proxyLiteFlag := flag.Bool("ip2proxy", false, "enable ip2proxy PX4-LITE generation")
15+
ip2proxyFlag := flag.Bool("ip2proxy-pro", false, "enable ip2proxy PX4 generation")
16+
ip2proxyLiteToken := flag.String("ip2proxy-token", "", "Get token here https://lite.ip2location.com/file-download")
17+
ip2proxyToken := flag.String("ip2proxy-pro-token", "", "ip2proxy download token")
18+
ip2proxyLiteFilename := flag.String("ip2proxy-lite-filename", "", "Filename of already downloaded ip2proxy-lite db")
19+
ip2proxyFilename := flag.String("ip2proxy-pro-filename", "", "Filename of already downloaded ip2proxy db")
20+
ip2proxyPrintType := flag.Bool("ip2proxy-print-type", false, "Print proxy type in map, instead of `1`")
1621
maxmind := flag.Bool("maxmind", false, "enable maxmind generation")
1722
maxmindIPVer := flag.Int("ipver", 4, "MaxMind ip version (4 or 6)")
1823
maxmindLang := flag.String("lang", "ru", "MaxMind city name language")
@@ -29,12 +34,13 @@ func main() {
2934
printMessage("ip2geo", "version "+VERSION, "OK")
3035
return
3136
}
32-
if !(*ipgeobase || *tor || *maxmind || *ip2proxyFlag) {
37+
if !(*ipgeobase || *tor || *maxmind || *ip2proxyLiteFlag || *ip2proxyFlag) {
3338
// By default, generate all maps
3439
*ipgeobase = true
3540
*tor = true
3641
*maxmind = true
37-
*ip2proxyFlag = *ip2proxyToken != ""
42+
*ip2proxyLiteFlag = *ip2proxyLiteToken != "" || *ip2proxyLiteFilename != ""
43+
*ip2proxyFlag = *ip2proxyToken != "" || *ip2proxyFilename != ""
3844
}
3945
if *quiet {
4046
logLevel = 1
@@ -82,12 +88,28 @@ func main() {
8288
go Generate(&m)
8389
}
8490

91+
if *ip2proxyLiteFlag {
92+
goroutinesCount++
93+
o := ip2proxy{
94+
Name: "ip2proxyLite",
95+
Token: *ip2proxyLiteToken,
96+
Filename: *ip2proxyLiteFilename,
97+
ErrorsChan: errorChannel,
98+
OutputDir: *outputDir,
99+
PrintType: *ip2proxyPrintType,
100+
}
101+
go o.Get()
102+
}
103+
85104
if *ip2proxyFlag {
86105
goroutinesCount++
87106
o := ip2proxy{
107+
Name: "ip2proxyPro",
88108
Token: *ip2proxyToken,
109+
Filename: *ip2proxyFilename,
89110
ErrorsChan: errorChannel,
90111
OutputDir: *outputDir,
112+
PrintType: *ip2proxyPrintType,
91113
}
92114
go o.Get()
93115
}

ip2proxy.go

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"archive/zip"
5+
"errors"
56
"fmt"
67
"io/ioutil"
78
"net"
@@ -23,43 +24,72 @@ type ip2proxyItem struct {
2324
}
2425

2526
type 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+
5888
func (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

88118
func (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

132162
func (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
}

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func printMessage(module, message, status string) {
6262
}
6363
statusMesage = color.BlueString(status)
6464
}
65-
fmt.Printf("%-10s | %-60s [%s]\n", module, message, statusMesage)
65+
fmt.Printf("%-15s | %-60s [%s]\n", module, message, statusMesage)
6666
}
6767

6868
func getIPRange(ipver int, network string) string {

0 commit comments

Comments
 (0)