mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
重构advLayer代码,完全使用通用接口.
This commit is contained in:
@@ -30,6 +30,8 @@ type CommonConf struct {
|
||||
|
||||
AdvancedLayer string `toml:"advancedLayer"` //高级层; 可不填,或者为ws,或者为grpc
|
||||
|
||||
IsEarly bool `toml:"early"` //是否启用 0-rtt
|
||||
|
||||
Path string `toml:"path"` //ws 的path 或 grpc的 serviceName。为了简便我们在同一位置给出.
|
||||
|
||||
Extra map[string]any `toml:"extra"` //用于包含任意其它数据.虽然本包自己定义的协议肯定都是已知的,但是如果其他人使用了本包的话,那就有可能添加一些 新协议 特定的数据.
|
||||
|
||||
@@ -295,11 +295,11 @@ func configCommonByURL(ser ProxyCommon, u *url.URL) {
|
||||
}
|
||||
}
|
||||
|
||||
//setAdvancedLayer
|
||||
func configCommon(ser ProxyCommon, cc *CommonConf) {
|
||||
ser.getCommon().setAdvancedLayer(cc.AdvancedLayer)
|
||||
//setAdvancedLayer, setPath
|
||||
func configCommon(this ProxyCommon, cc *CommonConf) {
|
||||
this.getCommon().setAdvancedLayer(cc.AdvancedLayer)
|
||||
if cc.Path != "" {
|
||||
ser.getCommon().setPath(cc.Path)
|
||||
this.getCommon().setPath(cc.Path)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -326,18 +326,7 @@ func configCommonForClient(cli ProxyCommon, dc *DialConf) error {
|
||||
|
||||
configCommon(cli, &dc.CommonConf)
|
||||
|
||||
switch dc.AdvancedLayer {
|
||||
case "ws":
|
||||
return cli.initWS_client()
|
||||
case "grpc":
|
||||
if dc.Extra != nil {
|
||||
if thing := dc.Extra["grpc_multi"]; thing != nil {
|
||||
if use, ok := thing.(bool); ok && use == true {
|
||||
clic.grpc_multi = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
clic.InitAdvLayer()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -361,25 +350,7 @@ func configCommonForServer(ser ProxyCommon, lc *ListenConf) error {
|
||||
serc.setHeader(lc.HttpHeader)
|
||||
}
|
||||
|
||||
switch lc.AdvancedLayer {
|
||||
case "ws":
|
||||
err := ser.initWS_server()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case "grpc":
|
||||
err := ser.initGRPC_server()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//case "quic":
|
||||
|
||||
//因为quic接管了tls层, 而我们的configCommonForServer是在tls配置之前被调用的
|
||||
// 所以不能在这里配置quic。本作直接在 prepareTLS_forServer 函数里配置 quic的所有配置
|
||||
|
||||
}
|
||||
serc.InitAdvLayer()
|
||||
|
||||
fallbackThing := lc.Fallback
|
||||
|
||||
|
||||
242
proxy/proxy.go
242
proxy/proxy.go
@@ -1,15 +1,13 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/e1732a364fed/v2ray_simple/advLayer/grpc"
|
||||
"github.com/e1732a364fed/v2ray_simple/advLayer/quic"
|
||||
"github.com/e1732a364fed/v2ray_simple/advLayer/ws"
|
||||
"github.com/e1732a364fed/v2ray_simple/advLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/httpLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/netLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/tlsLayer"
|
||||
@@ -151,14 +149,6 @@ type ProxyCommon interface {
|
||||
CantRoute() bool //for inServer
|
||||
GetTag() string
|
||||
|
||||
//如果 IsHandleInitialLayers 方法返回true, 则监听/拨号从传输层一直到高级层的过程直接由inServer/outClient自己处理, 而我们主过程直接处理它 处理完毕的剩下的 代理层。
|
||||
//
|
||||
// quic就属于这种接管底层协议的“超级协议”, 可称之为 SuperProxy。
|
||||
IsHandleInitialLayers() bool
|
||||
|
||||
// 在IsHandleInitialLayers时可用, 用于 inServer
|
||||
HandleInitialLayersFunc() func() (newConnChan chan net.Conn, baseConn any)
|
||||
|
||||
/////////////////// TLS层 ///////////////////
|
||||
|
||||
SetUseTLS()
|
||||
@@ -182,21 +172,10 @@ type ProxyCommon interface {
|
||||
|
||||
AdvancedLayer() string //如果使用了ws或者grpc,这个要返回 ws 或 grpc
|
||||
|
||||
GetWS_Client() *ws.Client //for outClient
|
||||
GetWS_Server() *ws.Server //for inServer
|
||||
GetAdvClient() advLayer.Client
|
||||
GetAdvServer() advLayer.Server
|
||||
|
||||
initWS_client() error //for outClient
|
||||
initWS_server() error //for inServer
|
||||
|
||||
GetGRPC_Server() *grpc.Server
|
||||
|
||||
IsGrpcClientMultiMode() bool
|
||||
|
||||
initGRPC_server() error
|
||||
|
||||
IsMux() bool //如果用了grpc或者quic, 则此方法返回true。这个是用于判断外层mux的。
|
||||
|
||||
GetQuic_Client() *quic.Client //for outClient
|
||||
//IsGrpcClientMultiMode() bool
|
||||
|
||||
/////////////////// 内层mux层 ///////////////////
|
||||
|
||||
@@ -234,17 +213,13 @@ type ProxyCommonStruct struct {
|
||||
|
||||
AdvancedL string
|
||||
|
||||
ws_c *ws.Client
|
||||
ws_s *ws.Server
|
||||
advC advLayer.Client
|
||||
advS advLayer.Server
|
||||
|
||||
//grpc_multi bool
|
||||
|
||||
grpc_s *grpc.Server
|
||||
grpc_multi bool
|
||||
FallbackAddr *netLayer.Addr
|
||||
|
||||
quic_c *quic.Client
|
||||
|
||||
listenCommonConnFunc func() (newConnChan chan net.Conn, baseConn any)
|
||||
|
||||
innermux *smux.Session //用于存储 client的已拨号的mux连接
|
||||
|
||||
}
|
||||
@@ -253,10 +228,6 @@ func (pcs *ProxyCommonStruct) getCommon() *ProxyCommonStruct {
|
||||
return pcs
|
||||
}
|
||||
|
||||
func (pcs *ProxyCommonStruct) setListenCommonConnFunc(f func() (newConnChan chan net.Conn, baseConn any)) {
|
||||
pcs.listenCommonConnFunc = f
|
||||
}
|
||||
|
||||
func (pcs *ProxyCommonStruct) Network() string {
|
||||
return pcs.network
|
||||
}
|
||||
@@ -403,10 +374,6 @@ func (s *ProxyCommonStruct) CanFallback() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) IsHandleInitialLayers() bool {
|
||||
return s.AdvancedL == "quic"
|
||||
}
|
||||
|
||||
func (pcs *ProxyCommonStruct) setTLS_Server(s *tlsLayer.Server) {
|
||||
pcs.tls_s = s
|
||||
}
|
||||
@@ -431,9 +398,10 @@ func (s *ProxyCommonStruct) SetAddrStr(a string) {
|
||||
func (s *ProxyCommonStruct) IsUseTLS() bool {
|
||||
return s.TLS
|
||||
}
|
||||
func (s *ProxyCommonStruct) IsGrpcClientMultiMode() bool {
|
||||
return s.grpc_multi
|
||||
}
|
||||
|
||||
//func (s *ProxyCommonStruct) IsGrpcClientMultiMode() bool {
|
||||
// return s.grpc_multi
|
||||
//}
|
||||
|
||||
func (s *ProxyCommonStruct) IsMux() bool {
|
||||
switch s.AdvancedL {
|
||||
@@ -443,10 +411,6 @@ func (s *ProxyCommonStruct) IsMux() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) HandleInitialLayersFunc() func() (newConnChan chan net.Conn, baseConn any) {
|
||||
return s.listenCommonConnFunc
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) SetUseTLS() {
|
||||
s.TLS = true
|
||||
}
|
||||
@@ -457,112 +421,106 @@ func (s *ProxyCommonStruct) setListenConf(lc *ListenConf) {
|
||||
func (s *ProxyCommonStruct) setDialConf(dc *DialConf) {
|
||||
s.dialConf = dc
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) GetQuic_Client() *quic.Client {
|
||||
return s.quic_c
|
||||
func (s *ProxyCommonStruct) GetAdvClient() advLayer.Client {
|
||||
return s.advC
|
||||
}
|
||||
func (s *ProxyCommonStruct) GetAdvServer() advLayer.Server {
|
||||
return s.advS
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) setQuic_Client(c *quic.Client) {
|
||||
s.quic_c = c
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) GetWS_Client() *ws.Client {
|
||||
return s.ws_c
|
||||
}
|
||||
func (s *ProxyCommonStruct) GetWS_Server() *ws.Server {
|
||||
return s.ws_s
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) GetGRPC_Server() *grpc.Server {
|
||||
return s.grpc_s
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) initWS_client() error {
|
||||
if s.dialConf == nil {
|
||||
return errors.New("initWS_client failed when no dialConf assigned")
|
||||
}
|
||||
path := s.dialConf.Path
|
||||
if path == "" { // 至少Path需要为 "/"
|
||||
path = "/"
|
||||
func (s *ProxyCommonStruct) InitAdvLayer() {
|
||||
if s.AdvancedL == "" {
|
||||
return
|
||||
}
|
||||
|
||||
var useEarlyData bool
|
||||
if s.dialConf.Extra != nil {
|
||||
if thing := s.dialConf.Extra["ws_earlydata"]; thing != nil {
|
||||
if use, ok := thing.(bool); ok && use {
|
||||
useEarlyData = true
|
||||
creator := advLayer.ProtocolsMap[s.AdvancedL]
|
||||
if creator == nil {
|
||||
utils.Error("InitAdvLayer failed, 2, " + s.AdvancedL)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ad, err := netLayer.NewAddr(s.Addr)
|
||||
if err != nil {
|
||||
utils.Error("InitAdvLayer failed, 3")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if dc := s.dialConf; dc != nil {
|
||||
|
||||
var Headers map[string][]string
|
||||
if dc.HttpHeader != nil {
|
||||
if dc.HttpHeader.Request != nil {
|
||||
Headers = dc.HttpHeader.Request.Headers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var c *ws.Client
|
||||
var e error
|
||||
advClient, err := creator.NewClientFromConf(&advLayer.Conf{
|
||||
Path: dc.Path,
|
||||
Host: dc.Host,
|
||||
IsEarly: dc.IsEarly,
|
||||
Addr: ad,
|
||||
Headers: Headers,
|
||||
TlsConf: &tls.Config{
|
||||
InsecureSkipVerify: dc.Insecure,
|
||||
NextProtos: dc.Alpn,
|
||||
ServerName: dc.Host,
|
||||
},
|
||||
Extra: dc.Extra,
|
||||
})
|
||||
if err != nil {
|
||||
utils.Error("InitAdvLayer failed, 4")
|
||||
|
||||
if s.header != nil {
|
||||
c, e = ws.NewClient(s.dialConf.GetAddrStr(), path, s.header.Request.Headers)
|
||||
|
||||
} else {
|
||||
c, e = ws.NewClient(s.dialConf.GetAddrStr(), path, nil)
|
||||
return
|
||||
}
|
||||
s.advC = advClient
|
||||
|
||||
}
|
||||
if e != nil {
|
||||
return utils.ErrInErr{ErrDesc: "initWS_client failed", ErrDetail: e}
|
||||
}
|
||||
|
||||
c.UseEarlyData = useEarlyData
|
||||
s.ws_c = c
|
||||
if lc := s.listenConf; lc != nil {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) initWS_server() error {
|
||||
if s.listenConf == nil {
|
||||
|
||||
return errors.New("initWS_server failed when no listenConf assigned")
|
||||
|
||||
}
|
||||
path := s.listenConf.Path
|
||||
if path == "" { // 至少Path需要为 "/"
|
||||
path = "/"
|
||||
}
|
||||
|
||||
var useEarlyData bool
|
||||
if s.listenConf.Extra != nil {
|
||||
if thing := s.listenConf.Extra["ws_earlydata"]; thing != nil {
|
||||
if use, ok := thing.(bool); ok && use {
|
||||
useEarlyData = true
|
||||
var Headers map[string][]string
|
||||
if lc.HttpHeader != nil {
|
||||
if lc.HttpHeader.Request != nil {
|
||||
Headers = lc.HttpHeader.Response.Headers
|
||||
}
|
||||
}
|
||||
|
||||
var certArray []tls.Certificate
|
||||
|
||||
if lc.TLSCert != "" && lc.TLSKey != "" {
|
||||
certArray, err = tlsLayer.GetCertArrayFromFile(lc.TLSCert, lc.TLSKey)
|
||||
|
||||
if err != nil {
|
||||
|
||||
if ce := utils.CanLogErr("can't create tls cert"); ce != nil {
|
||||
ce.Write(zap.String("cert", lc.TLSCert), zap.String("key", lc.TLSKey), zap.Error(err))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
advSer, err := creator.NewServerFromConf(&advLayer.Conf{
|
||||
Path: lc.Path,
|
||||
Host: lc.Host,
|
||||
IsEarly: lc.IsEarly,
|
||||
Addr: ad,
|
||||
Headers: Headers,
|
||||
TlsConf: &tls.Config{
|
||||
InsecureSkipVerify: lc.Insecure,
|
||||
NextProtos: lc.Alpn,
|
||||
ServerName: lc.Host,
|
||||
Certificates: certArray,
|
||||
},
|
||||
Extra: lc.Extra,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.advS = advSer
|
||||
}
|
||||
var wss *ws.Server
|
||||
if s.header != nil {
|
||||
wss = ws.NewServer(path, s.header.Response.Headers)
|
||||
|
||||
} else {
|
||||
wss = ws.NewServer(path, nil)
|
||||
|
||||
}
|
||||
wss.UseEarlyData = useEarlyData
|
||||
|
||||
s.ws_s = wss
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ProxyCommonStruct) initGRPC_server() error {
|
||||
if s.listenConf == nil {
|
||||
|
||||
return errors.New("initGRPC_server failed when no listenConf assigned")
|
||||
|
||||
}
|
||||
|
||||
serviceName := s.listenConf.Path
|
||||
if serviceName == "" { //不能为空
|
||||
|
||||
return errors.New("initGRPC_server failed, path must be specified")
|
||||
|
||||
}
|
||||
|
||||
s.grpc_s = grpc.NewServer(serviceName)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
"github.com/e1732a364fed/v2ray_simple/advLayer/quic"
|
||||
"github.com/e1732a364fed/v2ray_simple/httpLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/netLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/tlsLayer"
|
||||
"github.com/e1732a364fed/v2ray_simple/utils"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
//use dc.Host, dc.Insecure, dc.Utls, dc.Alpn.
|
||||
// 如果用到了quic,还会直接配置quic的client的所有设置.
|
||||
func prepareTLS_forClient(com ProxyCommon, dc *DialConf) error {
|
||||
alpnList := dc.Alpn
|
||||
|
||||
@@ -26,59 +19,8 @@ func prepareTLS_forClient(com ProxyCommon, dc *DialConf) error {
|
||||
|
||||
switch com.AdvancedLayer() {
|
||||
case "quic":
|
||||
na, e := netLayer.NewAddr(com.AddrStr())
|
||||
if e != nil {
|
||||
if ce := utils.CanLogErr("prepareTLS_forClient,quic,netLayer.NewAddr failed"); ce != nil {
|
||||
ce.Write(zap.Error(e))
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
clic.setNetwork("udp")
|
||||
var useHysteria, hysteria_manual, early bool
|
||||
var maxbyteCount int
|
||||
|
||||
if dc.Extra != nil {
|
||||
if thing := dc.Extra["congestion_control"]; thing != nil {
|
||||
if use, ok := thing.(string); ok && use == "hy" {
|
||||
useHysteria = true
|
||||
|
||||
if thing := dc.Extra["mbps"]; thing != nil {
|
||||
if mbps, ok := thing.(int64); ok && mbps > 1 {
|
||||
maxbyteCount = int(mbps) * 1024 * 1024 / 8
|
||||
|
||||
log.Println("Using Hysteria Congestion Control, max upload mbps: ", mbps)
|
||||
}
|
||||
} else {
|
||||
log.Println("Using Hysteria Congestion Control, max upload mbps: ", quic.Default_hysteriaMaxByteCount, "mbps")
|
||||
}
|
||||
|
||||
if thing := dc.Extra["hy_manual"]; thing != nil {
|
||||
if ismanual, ok := thing.(bool); ok {
|
||||
hysteria_manual = ismanual
|
||||
if ismanual {
|
||||
log.Println("Using Hysteria Manual Control Mode")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if thing := dc.Extra["quic_early"]; thing != nil {
|
||||
if use, ok := thing.(bool); ok && use {
|
||||
early = true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if len(alpnList) == 0 {
|
||||
alpnList = quic.AlpnList
|
||||
}
|
||||
|
||||
clic.setQuic_Client(quic.NewClient(&na, alpnList, dc.Host, dc.Insecure, useHysteria, maxbyteCount, hysteria_manual, early))
|
||||
return nil //quic直接接管了tls,所以不执行下面步骤
|
||||
|
||||
return nil
|
||||
case "grpc":
|
||||
has_h2 := false
|
||||
for _, a := range alpnList {
|
||||
@@ -96,7 +38,6 @@ func prepareTLS_forClient(com ProxyCommon, dc *DialConf) error {
|
||||
}
|
||||
|
||||
//use lc.Host, lc.TLSCert, lc.TLSKey, lc.Insecure, lc.Alpn.
|
||||
// 如果用到了quic,还会直接配置quic的server的所有设置.
|
||||
func prepareTLS_forServer(com ProxyCommon, lc *ListenConf) error {
|
||||
// 这里直接不检查 字符串就直接传给 tlsLayer.NewServer
|
||||
// 所以要求 cert和 key 不在程序本身目录 的话,就要给出完整路径
|
||||
@@ -111,85 +52,8 @@ func prepareTLS_forServer(com ProxyCommon, lc *ListenConf) error {
|
||||
case "quic":
|
||||
|
||||
serc.setNetwork("udp")
|
||||
return nil
|
||||
|
||||
if len(alpnList) == 0 {
|
||||
alpnList = quic.AlpnList
|
||||
}
|
||||
|
||||
var useHysteria, hysteria_manual, early bool
|
||||
var maxbyteCount int
|
||||
var maxStreamCountInOneSession int64
|
||||
|
||||
if lc.Extra != nil {
|
||||
|
||||
if thing := lc.Extra["quic_early"]; thing != nil {
|
||||
if use, ok := thing.(bool); ok && use {
|
||||
early = true
|
||||
}
|
||||
}
|
||||
|
||||
if thing := lc.Extra["maxStreamCountInOneSession"]; thing != nil {
|
||||
if count, ok := thing.(int64); ok && count > 0 {
|
||||
log.Println("maxStreamCountInOneSession,", count)
|
||||
maxStreamCountInOneSession = count
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if thing := lc.Extra["congestion_control"]; thing != nil {
|
||||
if use, ok := thing.(string); ok && use == "hy" {
|
||||
useHysteria = true
|
||||
|
||||
if thing := lc.Extra["mbps"]; thing != nil {
|
||||
if mbps, ok := thing.(int64); ok && mbps > 1 {
|
||||
maxbyteCount = int(mbps) * 1024 * 1024 / 8
|
||||
|
||||
log.Println("Using Hysteria Congestion Control, max upload mbps: ", mbps)
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
log.Println("Using Hysteria Congestion Control, max upload mbps:", quic.Default_hysteriaMaxByteCount, "mbps")
|
||||
|
||||
}
|
||||
|
||||
if thing := lc.Extra["hy_manual"]; thing != nil {
|
||||
if ismanual, ok := thing.(bool); ok {
|
||||
hysteria_manual = ismanual
|
||||
if ismanual {
|
||||
log.Println("Using Hysteria Manual Control Mode")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
serc.setListenCommonConnFunc(func() (newConnChan chan net.Conn, baseConn any) {
|
||||
|
||||
certArray, err := tlsLayer.GetCertArrayFromFile(lc.TLSCert, lc.TLSKey)
|
||||
|
||||
if err != nil {
|
||||
|
||||
if ce := utils.CanLogErr("can't create tls cert"); ce != nil {
|
||||
ce.Write(zap.String("cert", lc.TLSCert), zap.String("key", lc.TLSKey), zap.Error(err))
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return quic.ListenInitialLayers(com.AddrStr(), tls.Config{
|
||||
InsecureSkipVerify: lc.Insecure,
|
||||
ServerName: lc.Host,
|
||||
Certificates: certArray,
|
||||
NextProtos: alpnList,
|
||||
}, useHysteria, maxbyteCount, hysteria_manual, early, maxStreamCountInOneSession)
|
||||
|
||||
})
|
||||
|
||||
return nil //quic直接接管了tls,所以不执行下面步骤
|
||||
case "grpc":
|
||||
has_h2 := false
|
||||
for _, a := range alpnList {
|
||||
|
||||
Reference in New Issue
Block a user