Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
fix #24
Browse files Browse the repository at this point in the history
  • Loading branch information
Sansui233 committed Jun 18, 2021
1 parent 3a7a415 commit 1fdd6cf
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 107 deletions.
2 changes: 1 addition & 1 deletion api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/gin-gonic/gin"
)

const version = "v0.7.0"
const version = "v0.7.3"

var router *gin.Engine

Expand Down
5 changes: 5 additions & 0 deletions doc/WhatsNew.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2021-06-19(v0.3.1)
- 忽略source的TLS证书校验 #24
- http请求添加timeout #24
- fix: 一个url请求失败后后续全都失败 #24

2021-06-03
- 添加健康检测并发数设置(v0.3.0)
- 更新依赖,修复 send on closed channel
Expand Down
145 changes: 145 additions & 0 deletions internal/app/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package app

import (
"crypto/tls"
"encoding/json"
"errors"
"github.com/Sansui233/proxypool/pkg/proxy"
"github.com/Sansui233/proxypoolCheck/config"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)

func getAllProxies() (proxy.ProxyList, error) {
var proxylist proxy.ProxyList
var errs []error // collect errors

for _, value := range config.Config.ServerUrl {
url := formatURL(value)
pjson, err := getProxies(url)
if err != nil {
log.Printf("Error when fetch %s: %s\n", url, err.Error())
errs = append(errs, err)
continue
}
log.Printf("Get %s line count: %d\n", url, len(pjson))

for i, p := range pjson {
if i == 0 || len(p) < 2 {
continue
}
p = p[2:] // remove "- "

if pp, ok := convert2Proxy(p); ok {
if i == 1 && pp.BaseInfo().Name == "NULL" {
log.Println("no proxy on " + url)
errs = append(errs, errors.New("no proxy on "+url))
continue
}
if config.Config.ShowRemoteSpeed == true {
name := strings.Replace(pp.BaseInfo().Name, " |", "_", 1)
pp.SetName(name)
}
proxylist = append(proxylist, pp)
}
}
}

if proxylist == nil {
if errs != nil {
errInfo := "\n"
for _, e := range errs {
errInfo = errInfo + e.Error() + ";\n"
}
return nil, errors.New(errInfo)
}
return nil, errors.New("no proxy")
}
return proxylist, nil
}

func formatURL(value string) string {
url := "http://127.0.0.1:8080"
if value != "http://127.0.0.1:8080" {
url = value
if url[len(url)-1] == '/' {
url = url[:len(url)-1]
}
}
urls := strings.Split(url, "/")
if urls[len(urls)-2] != "clash" {
url = url + "/clash/proxies"
}
return url
}

// get proxy strings from url
func getProxies(url string) ([]string, error) {
//resp, err := http.Get(url)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}

client := &http.Client{
Timeout: 5 * time.Second,
Transport: tr,
}
resp, err := client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
proxyJson := strings.Split(string(body), "\n")
if len(proxyJson) < 2 {
return nil, errors.New("no proxy on " + url)
}
return proxyJson, nil
}

// Convert json string(clash format) to proxy
func convert2Proxy(pjson string) (proxy.Proxy, bool) {
var f interface{}
err := json.Unmarshal([]byte(pjson), &f)
if err != nil {
return nil, false
}
jsnMap := f.(interface{}).(map[string]interface{})

switch jsnMap["type"].(string) {
case "ss":
var p proxy.Shadowsocks
err := json.Unmarshal([]byte(pjson), &p)
if err != nil {
return nil, false
}
return &p, true
case "ssr":
var p proxy.ShadowsocksR
err := json.Unmarshal([]byte(pjson), &p)
if err != nil {
return nil, false
}
return &p, true
case "vmess":
var p proxy.Vmess
err := json.Unmarshal([]byte(pjson), &p)
if err != nil {
return nil, false
}
return &p, true
case "trojan":
var p proxy.Trojan
err := json.Unmarshal([]byte(pjson), &p)
if err != nil {
return nil, false
}
return &p, true
}
return nil, false
}
106 changes: 0 additions & 106 deletions internal/app/task.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package app

import (
"encoding/json"
"errors"
"fmt"
"github.com/Sansui233/proxypool/pkg/healthcheck"
"github.com/Sansui233/proxypool/pkg/provider"
"github.com/Sansui233/proxypool/pkg/proxy"
"github.com/Sansui233/proxypoolCheck/config"
"github.com/Sansui233/proxypoolCheck/internal/cache"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)

Expand All @@ -21,7 +15,6 @@ var location, _ = time.LoadLocation("PRC")
// Get all usable proxies from proxypool server and set app vars
func InitApp() error{
// Get proxies from server
// TODO 可能是编译的问题,加了flag的版本节点会更少
proxies, err := getAllProxies()
if err != nil {
log.Println("Get proxies error: ", err)
Expand Down Expand Up @@ -73,102 +66,3 @@ func InitApp() error{
return nil
}

func getAllProxies() (proxy.ProxyList, error){
var proxylist proxy.ProxyList
tempMap := map[string]byte{}
tempMapLen:=len(tempMap)

for _, value:=range config.Config.ServerUrl{
url := "http://127.0.0.1:8080"
if value != "http://127.0.0.1:8080"{
url = value
if url[len(url)-1] == '/' {
url = url[:len(url)-1]
}
}
urls := strings.Split(url,"/")
if urls[len(urls)-2] != "clash" {
url = url + "/clash/proxies"
}
resp, err := http.Get(url)
if err != nil{
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
pjson := strings.Split(string(body),"\n")
if len(pjson) < 2{
return nil, errors.New("No proxy on remote server")
}


for i, pstr := range pjson {
if i == 0 || len(pstr)<2{
continue
}
tempMap[pstr]=0
if len(tempMap) ==tempMapLen{
continue
}
tempMapLen++
pstr = pstr[2:]
if pp, ok := convert2Proxy(pstr); ok{
if i == 1 && pp.BaseInfo().Name == "NULL" {
return nil, errors.New("No proxy on remote server")
}
if config.Config.ShowRemoteSpeed == true {
name := strings.Replace(pp.BaseInfo().Name, " |", "_",1)
pp.SetName(name)
}
proxylist = append(proxylist, pp)
}
}
}

if proxylist == nil {
return nil, errors.New("No Proxy")
}
return proxylist, nil
}

// Convert json string(clash format) to proxy
func convert2Proxy(pjson string) (proxy.Proxy, bool) {
var f interface{}
err := json.Unmarshal([]byte(pjson), &f)
if err != nil {
return nil, false
}
jsnMap := f.(interface{}).(map[string]interface{})

switch jsnMap["type"].(string) {
case "ss":
var p proxy.Shadowsocks
err := json.Unmarshal([]byte(pjson), &p)
if err != nil{
return nil, false
}
return &p, true
case "ssr":
var p proxy.ShadowsocksR
err := json.Unmarshal([]byte(pjson), &p)
if err != nil{
return nil, false
}
return &p, true
case "vmess":
var p proxy.Vmess
err := json.Unmarshal([]byte(pjson), &p)
if err != nil{
return nil, false
}
return &p, true
case "trojan":
var p proxy.Trojan
err := json.Unmarshal([]byte(pjson), &p)
if err != nil{
return nil, false
}
return &p, true
}
return nil, false
}

0 comments on commit 1fdd6cf

Please sign in to comment.