Skip to content

Commit 3165718

Browse files
authored
Ver 1.1
Added checking for parsed IP address belongs to a country
1 parent cc9b978 commit 3165718

File tree

3 files changed

+88
-21
lines changed

3 files changed

+88
-21
lines changed

config.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
{
2-
"ssconfigfile":"/etc/shadowsocks/config.json",
2+
"ssconfigfile":"/ss/config.json",
33
"sspath":"/bin/systemctl",
44
"ssrestartcommand":[
55
"restart",
6-
"sslocal.service"
6+
"cups.service"
77
],
88
"ssconfigsectionpath":[
99
"servers"
1010
],
1111
"ssserverseditpos":1,
1212
"sstimeoutdefault":0,
13-
"outputfile":"/etc/sskeyparser/parsingresult.json",
13+
"outputfile":"./parsingresult.json",
1414
"links":[
1515
{
16-
"url":"https://t.me/some_channel_with_keys",
16+
"url":"https://t.me/s/outlinevpn_ru",
1717
"mask":[
1818
"ss://"
1919
],
20-
"configcount":3,
20+
"configcount":2,
2121
"parsetoptobot":false
2222
},
2323
{
24-
"url":"https://www.some_site_with_keys.com/",
24+
"url":"https://t.me/s/keysOutline",
2525
"mask":[
2626
"ss://"
2727
],
28-
"configcount":1,
29-
"parsetoptobot":true
28+
"configcount":2,
29+
"parsetoptobot":false
3030
}
31-
]
31+
],
32+
"ipcheckserver":"https://ipinfo.io/",
33+
"ipcheckkey":"country",
34+
"ipcheckvalue":"RU"
3235
}

ipchecker.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"strings"
9+
)
10+
11+
func isIpValid(site string, ip string, keyName string, value string) bool {
12+
request := strings.Builder{}
13+
request.WriteString(site)
14+
request.WriteString(ip)
15+
resp, err := http.Get(request.String())
16+
if err != nil {
17+
fmt.Println("Unable to connect to ip checker server:", err)
18+
return false
19+
} else if resp.StatusCode == 200 {
20+
defer resp.Body.Close()
21+
body, err := io.ReadAll(resp.Body)
22+
if err != nil {
23+
fmt.Println("Unable to read ip info:", err)
24+
return false
25+
} else {
26+
var answer map[string]interface{}
27+
err := json.Unmarshal([]byte(body), &answer)
28+
if err != nil {
29+
fmt.Println("Ошибка при разборе JSON:", err)
30+
return false
31+
}
32+
for key, val := range answer {
33+
if key == keyName {
34+
if val == value {
35+
return false
36+
} else {
37+
fmt.Println("Country ", val)
38+
return true
39+
}
40+
}
41+
}
42+
}
43+
} else {
44+
fmt.Println("Service unavailable :", err)
45+
}
46+
return false
47+
}

sskeyparser.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type Config struct {
3737
SsTimeOutDefault int32
3838
OutputFile string
3939
Links []Link
40+
IpCheckServer string
41+
IpCheckKey string
42+
IpCheckValue string
4043
}
4144

4245
type SsConfigs struct {
@@ -52,31 +55,31 @@ type SsServerConf struct {
5255
//Mode string `json:"mode,omitempty"`
5356
}
5457

55-
func decodeSsServerConfig(str string) {
58+
func decodeSsServerConfig(str string) bool {
5659
var datastr string
5760
index := strings.IndexByte(str, '@')
5861
if index == -1 { // fully encoded string
5962
data, err := base64.StdEncoding.DecodeString(str)
6063
if err != nil {
6164
fmt.Println("error:", err)
62-
return
65+
return false
6366
}
6467
datastr = string(data[:])
6568
} else { // encoded only method:password
6669
shortstr := str[:index]
6770
data, err := base64.StdEncoding.DecodeString(shortstr)
6871
if err != nil {
6972
fmt.Println("error:", err)
70-
return
73+
return false
7174
}
7275
datastr = string(data[:]) + str[index:]
7376
}
7477
errstr := createSsServerConfig(datastr)
7578
if errstr != "" {
7679
fmt.Println(errstr)
77-
} else {
78-
80+
return false
7981
}
82+
return true
8083
}
8184

8285
func createSsServerConfig(str string) (errstr string) { //, errcode int
@@ -117,6 +120,11 @@ func createSsServerConfig(str string) (errstr string) { //, errcode int
117120
return errString //, 3
118121
} else {
119122
conf.Server = spstr[:index]
123+
//check ip
124+
if !isIpValid(config.IpCheckServer, conf.Server, config.IpCheckKey, config.IpCheckValue) {
125+
return "Ip is invalid"
126+
}
127+
//
120128
i, err := strconv.Atoi(spstr[index+1:])
121129
if err != nil {
122130
errString := "Invalid format of port " + spstr
@@ -173,18 +181,22 @@ func parseUp(link Link, body string) {
173181
_mask := body[i : i+lm]
174182
if mask == _mask {
175183
c := i + lm
184+
var added bool
176185
for c <= lastPos {
177186
if body[c] == '#' { // || body[c] == '?'
178187
str := body[i+lm : c]
179188
if mask == "ss://" {
180-
decodeSsServerConfig(str)
189+
added = decodeSsServerConfig(str)
181190
break
182191
}
183-
192+
// if mask == "vless://"
184193
}
185194
c++
186195
}
187-
count = count - 1
196+
if added {
197+
count = count - 1
198+
}
199+
188200
i = i - 10
189201
lastPos = i
190202
}
@@ -209,17 +221,22 @@ func parseDown(link Link, body string) {
209221
_mask := body[i : i+lm]
210222
if mask == _mask {
211223
c := i + lm
224+
var added bool
212225
for c <= lastPos {
213226
if body[c] == '#' { // || body[c] == '?'
214227
str := body[i+lm : c]
215228
if mask == "ss://" {
216-
decodeSsServerConfig(str)
229+
added = decodeSsServerConfig(str)
217230
break
218231
}
232+
// if mask == "vless://"
219233
}
220234
c++
221235
}
222-
count = count - 1
236+
if added {
237+
count = count - 1
238+
}
239+
223240
i = c
224241
//lastPos = i
225242
}
@@ -303,7 +320,7 @@ func main() {
303320
}
304321
var waitgroup sync.WaitGroup
305322
resultFile, err := os.Create(config.OutputFile)
306-
if err != nil { //
323+
if err != nil { // если возникла ошибка
307324
fmt.Println("Unable to create file:", err)
308325
}
309326
defer resultFile.Close()
@@ -348,7 +365,7 @@ func RestartSs() {
348365
func setSsServiceConfig(path string, middle []byte) bool {
349366
if fileExists(path) {
350367
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, os.ModePerm)
351-
if err != nil { //
368+
if err != nil { // если возникла ошибка
352369
fmt.Println("Unable to open file:", err)
353370
return false
354371
}

0 commit comments

Comments
 (0)