Skip to content

Commit

Permalink
feat(修复配置文件 map 并发读写问题):
Browse files Browse the repository at this point in the history
fixed:concurrent map read and map write
import copier
  • Loading branch information
bineyond committed May 4, 2024
1 parent ff9cf32 commit f2397cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/native v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jasonlvhit/gocron v0.0.1 h1:qTt5qF3b3srDjeOIR4Le1LfeyvoYzJlYpqvG7tJX5YU=
github.com/jasonlvhit/gocron v0.0.1/go.mod h1:k9a3TV8VcU73XZxfVHCHWMWF9SOqgoku0/QlY2yvlA4=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand Down
66 changes: 36 additions & 30 deletions internal/app/best_proxy_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync"
"time"

"github.com/jinzhu/copier"

"github.com/One-Piecs/proxypool/internal/cache"

"github.com/One-Piecs/proxypool/config"
Expand Down Expand Up @@ -176,6 +178,10 @@ func SubNiceProxyIp(format string, distNodeCountry string, proxyCountryIsoCode s

proxyCountryIsoCodeList := strings.Split(proxyCountryIsoCode, ",")

var proxyInfo config.ProxyInfo

_ = copier.Copy(&proxyInfo, &config.Config.ProxyInfo)

for _, node := range bestNodeList {

if !filterIpCountry(proxyCountryIsoCodeList, node.Country) {
Expand All @@ -184,17 +190,17 @@ func SubNiceProxyIp(format string, distNodeCountry string, proxyCountryIsoCode s

if f.Surge {
if f.Vmess {
genSurgeVmessUrl(&buf, distNodeCountry, node.Country, node.Ip, node.Port)
genSurgeVmessUrl(&buf, proxyInfo, distNodeCountry, node.Country, node.Ip, node.Port)
} else if f.Trojan {
genSurgeTrojanUrl(&buf, distNodeCountry, node.Country, node.Ip, node.Port)
genSurgeTrojanUrl(&buf, proxyInfo, distNodeCountry, node.Country, node.Ip, node.Port)
}
} else if f.Clash {
if f.Vmess {
genClashVmessUrl(&buf, distNodeCountry, node.Country, node.Ip, node.Port)
genClashVmessUrl(&buf, proxyInfo, distNodeCountry, node.Country, node.Ip, node.Port)
} else if f.Trojan {
genClashTrojanUrl(&buf, distNodeCountry, node.Country, node.Ip, node.Port)
genClashTrojanUrl(&buf, proxyInfo, distNodeCountry, node.Country, node.Ip, node.Port)
} else if f.Vless {
genClashVlessUrl(&buf, distNodeCountry, node.Country, node.Ip, node.Port)
genClashVlessUrl(&buf, proxyInfo, distNodeCountry, node.Country, node.Ip, node.Port)
}
}
}
Expand Down Expand Up @@ -275,52 +281,52 @@ func removeDuplicateElement(languages []string) []string {
return result
}

func genSurgeVmessUrl(buf *strings.Builder, nodeCountry, country, ip string, port int) {
func genSurgeVmessUrl(buf *strings.Builder, proxyInfo config.ProxyInfo, nodeCountry, country, ip string, port int) {
buf.WriteString(fmt.Sprintf(`%s %-15s = vmess, %-15s, %d, username=%v, sni=%v, ws=true, ws-path=%v, ws-headers=Ip:"%v", vmess-aead=true, tls=true
`,
country, ip, ip, port,
config.Config.ProxyInfo[nodeCountry]["vmess"]["uuid"],
config.Config.ProxyInfo[nodeCountry]["vmess"]["host"],
config.Config.ProxyInfo[nodeCountry]["vmess"]["path"],
config.Config.ProxyInfo[nodeCountry]["vmess"]["host"]))
proxyInfo[nodeCountry]["vmess"]["uuid"],
proxyInfo[nodeCountry]["vmess"]["host"],
proxyInfo[nodeCountry]["vmess"]["path"],
proxyInfo[nodeCountry]["vmess"]["host"]))
}

func genSurgeTrojanUrl(buf *strings.Builder, nodeCountry, country, ip string, port int) {
func genSurgeTrojanUrl(buf *strings.Builder, proxyInfo config.ProxyInfo, nodeCountry, country, ip string, port int) {
buf.WriteString(fmt.Sprintf(`%s %-15s = trojan, %-15s, %d, password=%v, sni=%v, ws=true, ws-path=%v, ws-headers=Ip:"%v"
`,
country, ip, ip, port,
config.Config.ProxyInfo[nodeCountry]["trojan"]["password"],
config.Config.ProxyInfo[nodeCountry]["trojan"]["host"],
config.Config.ProxyInfo[nodeCountry]["trojan"]["path"],
config.Config.ProxyInfo[nodeCountry]["trojan"]["host"]))
proxyInfo[nodeCountry]["trojan"]["password"],
proxyInfo[nodeCountry]["trojan"]["host"],
proxyInfo[nodeCountry]["trojan"]["path"],
proxyInfo[nodeCountry]["trojan"]["host"]))
}

func genClashVlessUrl(buf *strings.Builder, nodeCountry, country, ip string, port int) {
func genClashVlessUrl(buf *strings.Builder, proxyInfo config.ProxyInfo, nodeCountry, country, ip string, port int) {
buf.WriteString(fmt.Sprintf(` - {"name":"%s %-15s", "type":"vless", "server":"%s", "port":%d, "uuid":"%v", "network":"ws", "tls":true, "udp":true, "sni":"%v", "client-fingerprint":"chrome", "ws-opts":{"path":"%v", "headers":{"Ip":"%v"}}}
`,
country, ip, ip, port,
config.Config.ProxyInfo[nodeCountry]["vless"]["uuid"],
config.Config.ProxyInfo[nodeCountry]["vless"]["host"],
config.Config.ProxyInfo[nodeCountry]["vless"]["path"],
config.Config.ProxyInfo[nodeCountry]["vless"]["host"]))
proxyInfo[nodeCountry]["vless"]["uuid"],
proxyInfo[nodeCountry]["vless"]["host"],
proxyInfo[nodeCountry]["vless"]["path"],
proxyInfo[nodeCountry]["vless"]["host"]))
}

func genClashVmessUrl(buf *strings.Builder, nodeCountry, country, ip string, port int) {
func genClashVmessUrl(buf *strings.Builder, proxyInfo config.ProxyInfo, nodeCountry, country, ip string, port int) {
buf.WriteString(fmt.Sprintf(` - {"name":"%s %-15s", "type":"vmess", "server":"%s", "port":%d, "uuid":"%v", "tls":true, "cipher":"none", "alterId":0, "network":"ws", "ws-opts":{"path":"%v", "headers":{"Ip":"%v"}}, "servername":"%v"}
`,
country, ip, ip, port,
config.Config.ProxyInfo[nodeCountry]["vmess"]["uuid"],
config.Config.ProxyInfo[nodeCountry]["vmess"]["path"],
config.Config.ProxyInfo[nodeCountry]["vmess"]["host"],
config.Config.ProxyInfo[nodeCountry]["vmess"]["host"]))
proxyInfo[nodeCountry]["vmess"]["uuid"],
proxyInfo[nodeCountry]["vmess"]["path"],
proxyInfo[nodeCountry]["vmess"]["host"],
proxyInfo[nodeCountry]["vmess"]["host"]))
}

func genClashTrojanUrl(buf *strings.Builder, node_country, country, ip string, port int) {
func genClashTrojanUrl(buf *strings.Builder, proxyInfo config.ProxyInfo, node_country, country, ip string, port int) {
buf.WriteString(fmt.Sprintf(` - {"name":"%s %-15.15s", "type":"trojan", "server":"%s", "port":%d, "password":"%v", "sni":"%v", "network":"ws", "ws-opts":{"path":"%v", "headers":{"Ip":"%v"}}}
`,
country, ip, ip, port,
config.Config.ProxyInfo[node_country]["trojan"]["password"],
config.Config.ProxyInfo[node_country]["trojan"]["host"],
config.Config.ProxyInfo[node_country]["trojan"]["path"],
config.Config.ProxyInfo[node_country]["trojan"]["host"]))
proxyInfo[node_country]["trojan"]["password"],
proxyInfo[node_country]["trojan"]["host"],
proxyInfo[node_country]["trojan"]["path"],
proxyInfo[node_country]["trojan"]["host"]))
}

0 comments on commit f2397cf

Please sign in to comment.