v2raypool

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 37 Imported by: 0

README

简介

同时运行多个v2ray代理,暴露多个本地IP端口,组成简单的IP代理池。

可以选一个节点设为系统代理,用来浏览网页。要求不高的话,也可同时调用多个,作为爬虫切换IP的代理池。

提供通用的gRPC控制接口,参看数据定义文件 v2raypool.proto

使用说明

不想自己编译项目源码,可下载Release压缩包直接使用,再看第4-5步的使用说明。

1. 下载依赖

运行命令: go mod tidy

1.1 如因网络问题下载失败,可设置模块代理。运行命令:

go env -w GOPROXY=https://goproxy.cn,direct
# 或者 go env -w GOPROXY=https://goproxy.io,direct

1.2 若出现依赖包版本冲突,请删除 go.mod 文件,再运行命令:

go mod init github.com/iotames/v2raypool
go mod tidy

2. 编译可执行文件

2.1 编译

# 进入项目 main 目录,并执行go编译命令
cd main

# linux或mac 运行: go build -o v2raypool -trimpath -ldflags "-s -w -buildid=" .
go build -o v2raypool.exe -trimpath -ldflags "-s -w -buildid=" .

编译出二进制可执行文件 v2raypool(linux or max) 或 v2raypool.exe(windows)

2.2 生成配置文件

命令行运行可执行文件(v2raypool.exe 或 ./v2raypool),会生成配置文件 .env。并提示找不到v2ray核心文件:

v2raypool.exe
请检查配置文件,路径:(.env)
VP_V2RAY_PATH 配置项错误,找不到可执行文件。
请下载v2ray核心文件(https://github.com/v2fly/v2ray-core/releases)

3. 下载v2ray核心文件

3.1 官网下载核心文件Zip压缩包: https://github.com/v2fly/v2ray-core/releases

3.2 解压到 main/bin 目录,并删除或改名解压后的 config.json 文件,防止程序错误读取。

3.3 检查或修改v2ray 可执行文件路径: 查看 .env 配置文件的 VP_V2RAY_PATH 配置项。

4. 设置订阅地址

更改 .env 配置文件的 VP_SUBSCRIBE_URL,改成实际使用的订阅源地址(http开头) 若订阅源地址网络异常,可使用 VP_SUBSCRIBE_DATA_FILE 配置项。通过其他途径查看订阅地址的响应结果,把内容存入文件。

VP_HTTP_PROXY 配置项,可设置一个http开头的代理地址。在 gRPC客户端 使用 --activeproxynode 命令项可激活一个节点使用代理端口。

5. 运行服务端和客户端

5.1 服务端

可执行文件直接运行,启动 gRPC 服务端

# linux 或 mac 执行 ./v2raypool
v2raypool.exe

5.2 客户端

gRPC 客户端交互命令:

# 启动v2ray代理池
v2raypool.exe --startproxynodes

# 查看v2ray代理池信息(包括:本地代理端口号,测速结果,运行状态,测速时间,节点名,节点索引)
v2raypool.exe --getproxynodes

# 测速(测速基准使用https://www.google.com)。测速结束后,会自动选择最快的节点作为系统代理节点。
v2raypool.exe --testproxynodes

# 根据索引值激活某个节点为系统代理的端口(--getproxynodes 查看索引值,系统代理端口从VP_HTTP_PROXY的值读取)
v2raypool.exe --activeproxynode=16

# 停止所有节点
v2raypool.exe --stopproxynodes

6. 配置systemd系统服务(Linux)

使用环境变量 VP_ENV_FILE 定义环境变量配置文件的路径。不设置默认为 .env

使用Linux自带的systemctl命令管理 v2raypool

  1. 新建 v2raypool.service 文件:
vim /usr/lib/systemd/system/v2raypool.service

v2raypool.service内容示例(/root/v2raypool/main 为可执行文件所在路径):

[Unit]
Description=v2ray proxy pool
After=network.target

[Service]
WorkingDirectory=/root/v2raypool/main
ExecStart=/root/v2raypool/main/v2raypool
User=root
Restart=on-failure
RestartSec=300
# KillSignal=SIGQUIT
TimeoutStopSec=10
StandardOutput=file:/root/v2raypool/main/output.log
# StandardError=file:/root/qddns/output.err.log

[Install]
WantedBy=multi-user.target
  1. 重载systemd配置
systemctl daemon-reload
  1. 使用 systemctl 管理v2raypool的gRPC服务端
systemctl status v2raypool
systemctl start v2raypool
systemctl stop v2raypool

路由规则

支持自定义域名和IP列表配置:

  • PROXY_DOMAIN_LIST 代理域名列表
  • DIRECT_DOMAIN_LIST 直连域名列表
  • PROXY_IP_LIST 代理IP列表
  • DIRECT_IP_LIST 直连IP列表

域名匹配规则:

  • 纯字符串:当此字符串匹配目标域名中任意部分,该规则生效。比如 sina.com 可以匹配 sina.comsina.com.cnsina.companywww.sina.com,但不匹配 sina.cn
  • 正则表达式:由 regexp: 开始,余下部分是一个正则表达式。当此正则表达式匹配目标域名时,该规则生效。例如 regexp:\.goo.*\.com$`` 匹配 www.google.comfonts.googleapis.com,但不匹配 google.com`。
  • 子域名(推荐):由 domain: 开始,余下部分是一个域名。当此域名是目标域名或其子域名时,该规则生效。例如 domain:v2ray.com 匹配 www.v2ray.comv2ray.com,但不匹配 xv2ray.com
  • 完整匹配:由 full: 开始,余下部分是一个域名。当此域名完整匹配目标域名时,该规则生效。例如 full:v2ray.com 匹配 v2ray.com 但不匹配 www.v2ray.com
  • 预定义域名列表:由 geosite: 开头,余下部分是一个类别名称(域名列表),如 geosite:google 或者 geosite:cn。名称及域名列表参考预定义域名列表
  • 从文件中加载域名:形如 ext:file:tag,必须以 ext: 开头,后面跟文件名和标签,文件存放在资源目录中,文件格式与 geosite.dat 相同,标签必须在文件中存在。

IP匹配规则:

  • IP:形如 127.0.0.1
  • CIDR:形如 10.0.0.0/8
  • GeoIP: 形如 geoip:cn 为正向匹配,即为匹配「中国大陆 IP 地址」。后面跟双字符国家或地区代码,支持所有可以上网的国家和地区。

形如 `geoip:!cn`` 为反向匹配,即为匹配「非中国大陆 IP 地址」。后面跟双字符国家或地区代码,支持所有可以上网的国家和地区。

特殊值:geoip:private(V2Ray 3.5+),包含所有私有地址,如 127.0.0.1

  • 从文件中加载 IP: 形如 ext:file:tagext-ip:file:tag 为正向匹配,即为匹配 「tag 内的 IP 地址」。

形如 ext:file:!tagext-ip:file:!tag 为反向匹配,即为匹配「非 tag 内的 IP 地址」。

必须以 ext:ext-ip: 开头,后面跟文件名、标签!标签,文件存放在资源目录中,文件格式与 geoip.dat 相同,标签必须在文件中存在。

具体请参看 v2ray路由规则

开发相关

gRPC接口

proto数据格式定义文件: ./v2raypool.proto gRPC接口文件目录: ./grpc

protoc --go_out=./ --go-grpc_out=./ product.proto

Documentation

Index

Constants

View Source
const ROUTING_RULES_FILE = "routing.rules.json"
View Source
const TAG_INBOUND_API = "TAG_INBOUND_API"
View Source
const TAG_OUTBOUND_ACTIVE = "TAG_ACTIVE_OUTBOUND"
View Source
const TAG_OUTBOUND_API = "TAG_OUTBOUND_API"
View Source
const TAG_OUTBOUND_DIRECT = "DIRECT"
View Source
const V2RAY_CONFIG_FILE = "v2ray.config.json"

Variables

This section is empty.

Functions

func Base64StdDecode

func Base64StdDecode(s string) (string, error)

封装base64.StdEncoding进行解码,加入了长度补全,换行删除。当error时,返回输入和err

func Base64URLDecode

func Base64URLDecode(s string) (string, error)

封装base64.URLEncoding进行解码,加入了长度补全,换行删除。当error时,返回输入和err

func ChangeProxyNode

func ChangeProxyNode() error

func NewProxyPoolGrpcClient

func NewProxyPoolGrpcClient() (c g.ProxyPoolServiceClient, conn *grpc.ClientConn)

NewProxyPoolGrpcClient c, conn := NewProxyPoolGrpcClient() defer conn.Close() ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() nds, err := c.GetProxyNodes(ctx, &ProxyNode{IsRunning: true})

func RequestProxyPoolGrpcOnce

func RequestProxyPoolGrpcOnce(h func(c g.ProxyPoolServiceClient, ctx context.Context))

func RunProxyPoolGrpcServer

func RunProxyPoolGrpcServer()

func StartServer

func StartServer()

Types

type ProxyNode

type ProxyNode struct {
	Index, LocalPort int
	// cmd               *exec.Cmd
	Id                string
	RemoteAddr, Title string
	TestUrl           string
	Speed             time.Duration
	TestAt            time.Time
	// contains filtered or unexported fields
}

func NewProxyNodeByV2ray

func NewProxyNodeByV2ray(vnd V2rayNode) *ProxyNode

func (*ProxyNode) AddToPool added in v1.1.1

func (p *ProxyNode) AddToPool(c *V2rayApiClient) error

func (*ProxyNode) GetId

func (p *ProxyNode) GetId() string

func (ProxyNode) IsOk

func (p ProxyNode) IsOk() bool

func (ProxyNode) IsRunning

func (p ProxyNode) IsRunning() bool

func (*ProxyNode) Remove added in v1.1.1

func (p *ProxyNode) Remove(c *V2rayApiClient, tag string) error

func (*ProxyNode) SetV2ray

func (p *ProxyNode) SetV2ray(n V2rayNode) *ProxyNode

type ProxyNodes

type ProxyNodes []ProxyNode

func GetRunningNodes

func GetRunningNodes() ProxyNodes

func (ProxyNodes) Len

func (s ProxyNodes) Len() int

func (ProxyNodes) Less

func (s ProxyNodes) Less(i, j int) bool

func (*ProxyNodes) SortBySpeed

func (s *ProxyNodes) SortBySpeed()

func (ProxyNodes) Swap

func (s ProxyNodes) Swap(i, j int)

type ProxyPool

type ProxyPool struct {
	IsLock bool
	// contains filtered or unexported fields
}

func GetProxyPool

func GetProxyPool() *ProxyPool

func NewProxyPool

func NewProxyPool() *ProxyPool

func (*ProxyPool) ActiveNode

func (p *ProxyPool) ActiveNode(n ProxyNode) error

ActiveNode TODO 另开一条线程

func (*ProxyPool) AddNode

func (p *ProxyPool) AddNode(n ProxyNode)

func (*ProxyPool) AddSpeedNode

func (p *ProxyPool) AddSpeedNode(key string, n ProxyNode)

func (ProxyPool) GetLocalAddr

func (p ProxyPool) GetLocalAddr(n ProxyNode) string

func (*ProxyPool) GetNodes

func (p *ProxyPool) GetNodes(domain string) ProxyNodes

func (*ProxyPool) InitSubscribeData

func (p *ProxyPool) InitSubscribeData() *ProxyPool

func (*ProxyPool) KillAllNodes

func (p *ProxyPool) KillAllNodes() (total, runport, kill, fail int)

func (*ProxyPool) RemoveNode

func (p *ProxyPool) RemoveNode(n ProxyNode)

func (*ProxyPool) SetCmd added in v1.1.1

func (p *ProxyPool) SetCmd(cmd *exec.Cmd)

func (*ProxyPool) SetLocalAddr

func (p *ProxyPool) SetLocalAddr(n *ProxyNode, port int) string

func (*ProxyPool) SetLocalPortStart

func (p *ProxyPool) SetLocalPortStart(port int) *ProxyPool

func (*ProxyPool) SetNodes

func (p *ProxyPool) SetNodes(nds []ProxyNode)

func (*ProxyPool) SetSubscribeRawData

func (p *ProxyPool) SetSubscribeRawData(d string) *ProxyPool

func (*ProxyPool) SetSubscribeUrl

func (p *ProxyPool) SetSubscribeUrl(d string) *ProxyPool

func (*ProxyPool) SetTestMaxDuration

func (p *ProxyPool) SetTestMaxDuration(d time.Duration) *ProxyPool

func (*ProxyPool) SetTestUrl

func (p *ProxyPool) SetTestUrl(turl string) *ProxyPool

func (*ProxyPool) SetV2rayPath

func (p *ProxyPool) SetV2rayPath(path string) *ProxyPool

func (*ProxyPool) StartAll

func (p *ProxyPool) StartAll() error

func (*ProxyPool) StopAll

func (p *ProxyPool) StopAll() error

func (*ProxyPool) TestAll

func (p *ProxyPool) TestAll()

func (*ProxyPool) TestAllForce

func (p *ProxyPool) TestAllForce()

func (*ProxyPool) UpdateAfterStopAll

func (p *ProxyPool) UpdateAfterStopAll()

func (*ProxyPool) UpdateNode

func (p *ProxyPool) UpdateNode(n ProxyNode) error

func (*ProxyPool) UpdateSubscribe

func (p *ProxyPool) UpdateSubscribe() (total, add int)

type ProxyPoolServer

type ProxyPoolServer struct {
	g.UnimplementedProxyPoolServiceServer
}

func (ProxyPoolServer) ActiveProxyNode

func (s ProxyPoolServer) ActiveProxyNode(ctx context.Context, req *g.ProxyNode) (result *g.OptResult, err error)

func (ProxyPoolServer) GetProxyNodes

func (s ProxyPoolServer) GetProxyNodes(ctx context.Context, req *g.ProxyNode) (*g.ProxyNodes, error)

func (ProxyPoolServer) GetProxyNodesByDomain

func (s ProxyPoolServer) GetProxyNodesByDomain(ctx context.Context, req *g.OptRequestDomain) (*g.ProxyNodes, error)

func (ProxyPoolServer) KillAllNodes

func (s ProxyPoolServer) KillAllNodes(ctx context.Context, req *g.OptRequest) (result *g.KillNodesResult, err error)

func (ProxyPoolServer) SetTestUrl

func (s ProxyPoolServer) SetTestUrl(ctx context.Context, req *g.OptRequestUrl) (result *g.OptResult, err error)

func (ProxyPoolServer) StartProxyPoolAll

func (s ProxyPoolServer) StartProxyPoolAll(ctx context.Context, req *g.OptRequest) (result *g.OptResult, err error)

func (ProxyPoolServer) StopProxyPoolAll

func (s ProxyPoolServer) StopProxyPoolAll(ctx context.Context, req *g.OptRequest) (result *g.OptResult, err error)

func (ProxyPoolServer) TestProxyPoolAll

func (s ProxyPoolServer) TestProxyPoolAll(ctx context.Context, req *g.OptRequest) (result *g.OptResult, err error)

func (ProxyPoolServer) TestProxyPoolAllForce

func (s ProxyPoolServer) TestProxyPoolAllForce(ctx context.Context, req *g.OptRequest) (result *g.OptResult, err error)

func (ProxyPoolServer) UpdateProxySubscribe

func (s ProxyPoolServer) UpdateProxySubscribe(ctx context.Context, req *g.OptRequest) (result *g.UpdateSubscribeResult, err error)

type V2rayApiClient added in v1.1.1

type V2rayApiClient struct {
	// contains filtered or unexported fields
}

func NewV2rayApiClientV5 added in v1.1.1

func NewV2rayApiClientV5(addr string) *V2rayApiClient

func (V2rayApiClient) AddInbound added in v1.1.1

func (a V2rayApiClient) AddInbound(inport net.Port, intag, protocol string) error

AddInbound 添加入站规则 protocol http|socks

func (V2rayApiClient) AddOutbound added in v1.1.1

func (a V2rayApiClient) AddOutbound(addr, port, nett, id, tls, outag string) error

func (V2rayApiClient) AddOutboundByV2rayNode added in v1.1.1

func (a V2rayApiClient) AddOutboundByV2rayNode(nd V2rayNode, outag string) error

func (*V2rayApiClient) Close added in v1.1.1

func (a *V2rayApiClient) Close()

func (*V2rayApiClient) Dial added in v1.1.1

func (a *V2rayApiClient) Dial() error

func (V2rayApiClient) RemoveInbound added in v1.1.1

func (a V2rayApiClient) RemoveInbound(intag string) error

func (V2rayApiClient) RemoveOutbound added in v1.1.1

func (a V2rayApiClient) RemoveOutbound(outag string) error

type V2rayConfigV4

type V2rayConfigV4 struct {
	Log json.RawMessage `json:"log"`
	Api json.RawMessage `json:"api"`
	// Dns       json.RawMessage `json:"dns"`
	Routing   json.RawMessage `json:"routing"`
	Inbounds  []V2rayInbound  `json:"inbounds"`
	Outbounds []V2rayOutbound `json:"outbounds"`
}

type V2rayConfigV5

type V2rayConfigV5 struct {
	Log        json.RawMessage            `json:"log"`
	Dns        json.RawMessage            `json:"dns"`
	Router     json.RawMessage            `json:"router"`
	Inbounds   []V2rayInbound             `json:"inbounds"`
	Outbounds  []V2rayOutbound            `json:"outbounds"`
	Services   map[string]json.RawMessage `json:"services"`
	Extensions []json.RawMessage          `json:"extension"`
}

type V2rayInbound

type V2rayInbound struct {
	Protocol string
	Port     int
	Listen   string          // 默认值为 "0.0.0.0"
	Tag      string          // 此入站连接的标识,用于在其它的配置中定位此连接。当其不为空时,其值必须在所有 tag 中唯一。
	Settings json.RawMessage `json:"settings"` // {"auth":"noauth","udp":true,"ip":"%s"}
}

v4 {"port":%d,"listen":"%s","protocol":"http","settings":{"auth":"noauth","udp":true,"ip":"%s"}}

type V2rayNode

type V2rayNode struct {
	Protocol, Add, Host, Id, Net, Path, Port, Ps, Tls, Type string
	V, Aid                                                  int
}

"protocol":"vmess"

func ParseV2rayNodes

func ParseV2rayNodes(data string) []V2rayNode

ParseNodes 解析节点 Add, Ps ... {"add":"jp6.v2u9.top","host":"","id":"0999AE93-1330-4A75-DBC1-0DD545F7DD60","net":"ws","path":"","port":"41444","ps":"u9un-v2-JP-Tokyo6(1)","tls":"","v":2,"aid":0,"type":"none"} {"add":"hk6.v2u9.top","host":"","id":"93EA57CE-EA21-7240-EE7F-317F4A6A8B65","net":"ws","path":"","port":"444","ps":"u9un-v2-HK-HongKong6","tls":"tls","type":"none","v":2,"aid":0}

type V2rayOutbound

type V2rayOutbound struct {
	Protocol    string          `json:"protocol"`
	SendThrough string          `json:"sendThrough"` // 用于发送数据的 IP 地址,当主机有多个 IP 地址时有效,默认值为 "0.0.0.0"。
	Tag         string          `json:"tag"`
	Settings    json.RawMessage `json:"settings"` // 视协议不同而不同。详见每个协议中的 OutboundConfigurationObject
	// "streamSettings":{"network":"%s","tlsSettings":{"disableSystemRoot":false},"wsSettings":{"path":""},"xtlsSettings":{"disableSystemRoot":false}}
	StreamSetting json.RawMessage `json:"streamSettings"`
}

type V2rayRouteRule

type V2rayRouteRule struct {
	DomainMatcher string   `json:"domainMatcher"`
	Type          string   `json:"type"`
	Domains       []string `json:"domains"`
	Ip            []string `json:"ip"`
	InboundTag    []string `json:"inboundTag"`
	OutboundTag   string   `json:"outboundTag"` //direct

}

V2rayRouteRule https://www.v2fly.org/config/routing.html#ruleobject

type V2rayServer

type V2rayServer struct {
	// contains filtered or unexported fields
}

func NewV2ray

func NewV2ray(v2rayPath string) *V2rayServer

func (*V2rayServer) Run

func (v *V2rayServer) Run()

func (*V2rayServer) SetNode

func (v *V2rayServer) SetNode(n V2rayNode) *V2rayServer

func (*V2rayServer) SetPort

func (v *V2rayServer) SetPort(port int) *V2rayServer

func (*V2rayServer) Start

func (v *V2rayServer) Start() (cmd *exec.Cmd, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL