mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-10-13 20:43:41 +08:00
feat:添加回落和分流功能.
创建新子包netLayer, 将 proxy.Addr改为 netLayer.Addr 修订文档 RoutePolicy等分流机制也放到 netLayer 引入github.com/oschwald/maxminddb-golang 依赖,支持使用 GeoLite2-Country.mmdb 来进行ip分流 另外注意它默认的版本对于 golang.org/x/sys 包的依赖太老了,会导致go1.18中编译不通过,我在 go.mod 文件中新增了下面代码,就能通过编译了 ``` require ( golang.org/x/sys v0.0.0-20220315194320-039c03cc5b86 // indirect ) ``` verysimple的可执行文件的相同目录下,必须有该mmdb文件才能够开启ip分流功能 新配置方式:配置文件新加一行 "route":{ "mycountry":"CN" } mycountry指定的国家的ip会被直连发送,其他地址ip会被发送到代理. 新配置方式,回落,直接在 local 项的 url 的 query部分添加 fallback=:80, 或者 fallback=127.0.0.1:80 即可 回落到指定端口. 将tls_test重新挪动到tlsLayer包中 在main.go中添加了 logLevel变量,并且把关于配置文件的部分挪动到 config.go 出了上面的分流和回落以外,还新增支持了 #xxx 的尾缀,用于配置该url的tag. tag在未来会被用于精准分流 Makefile中新增了 PACK 参数用于编译出 打包版的发行包;可选 tag=embed_geoip 参数用于将mmdb.tgz文件内置到可执行程序里 同时,我开始直接使用go1.18编译本项目,期待性能提升,因为这是新发布的版本,看了介绍据说对 mac m1有20%的提升.
This commit is contained in:
75
netLayer/geoip.go
Normal file
75
netLayer/geoip.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package netLayer
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"flag"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/oschwald/maxminddb-golang"
|
||||
)
|
||||
|
||||
var (
|
||||
the_geoipdb *maxminddb.Reader
|
||||
embedGeoip bool
|
||||
|
||||
GeoipFileName string //若运行程序指定了 geoip 参数,则该值为给定值;否则默认会被init为 GeoLite2-Country.mmdb
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&GeoipFileName, "geoip", "GeoLite2-Country.mmdb", "geoip maxmind file name")
|
||||
|
||||
}
|
||||
|
||||
func HasEmbedGeoip() bool {
|
||||
return embedGeoip
|
||||
}
|
||||
|
||||
func loadMaxmindGeoipBytes(bs []byte) {
|
||||
db, err := maxminddb.FromBytes(bs)
|
||||
if err != nil {
|
||||
log.Fatalln("loadMaxmindGeoipBytes", err)
|
||||
}
|
||||
the_geoipdb = db
|
||||
}
|
||||
|
||||
//将一个外部的文件加载为我们默认的 geoip文件;若fn=="",则会自动使用 GeoipFileName 的值
|
||||
func LoadMaxmindGeoipFile(fn string) {
|
||||
if fn == "" {
|
||||
fn = GeoipFileName
|
||||
}
|
||||
if fn == "" { //因为 GeoipFileName 是共有变量,所以可能会被设成"", 不排除脑残
|
||||
return
|
||||
}
|
||||
bs, e := os.ReadFile(fn)
|
||||
if e != nil {
|
||||
log.Fatalln("loadMaxmindGeoipBytes", e)
|
||||
}
|
||||
loadMaxmindGeoipBytes(bs)
|
||||
|
||||
}
|
||||
|
||||
//使用默认的 geoip文件,会调用 GetIP_ISO_byReader
|
||||
func GetIP_ISO(ip net.IP) string {
|
||||
if the_geoipdb == nil {
|
||||
return ""
|
||||
}
|
||||
return GetIP_ISO_byReader(the_geoipdb, ip)
|
||||
}
|
||||
|
||||
//返回 iso 3166 字符串, 见 https://dev.maxmind.com/geoip/legacy/codes?lang=en ,大写,两字节
|
||||
func GetIP_ISO_byReader(db *maxminddb.Reader, ip net.IP) string {
|
||||
|
||||
var record struct {
|
||||
Country struct {
|
||||
ISOCode string `maxminddb:"iso_code"`
|
||||
} `maxminddb:"country"`
|
||||
}
|
||||
|
||||
err := db.Lookup(ip, &record)
|
||||
if err != nil {
|
||||
log.Fatal(err) //不应该发生
|
||||
}
|
||||
return record.Country.ISOCode
|
||||
}
|
Reference in New Issue
Block a user