update handler options
This commit is contained in:
@ -8,13 +8,13 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gosocks5"
|
||||
"github.com/go-gost/gost/pkg/bypass"
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/common/util/ss"
|
||||
"github.com/go-gost/gost/pkg/handler"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -22,31 +22,47 @@ func init() {
|
||||
}
|
||||
|
||||
type ssHandler struct {
|
||||
bypass bypass.Bypass
|
||||
router *chain.Router
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
cipher core.Cipher
|
||||
router *chain.Router
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
options handler.Options
|
||||
}
|
||||
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
options := &handler.Options{}
|
||||
options := handler.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &ssHandler{
|
||||
bypass: options.Bypass,
|
||||
router: options.Router,
|
||||
logger: options.Logger,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ssHandler) Init(md md.Metadata) (err error) {
|
||||
if err := h.parseMetadata(md); err != nil {
|
||||
return err
|
||||
if err = h.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
if len(h.options.Auths) > 0 {
|
||||
method := h.options.Auths[0].Username()
|
||||
password, _ := h.options.Auths[0].Password()
|
||||
h.cipher, err = ss.ShadowCipher(method, password, h.md.key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
h.router = &chain.Router{
|
||||
Retries: h.options.Retries,
|
||||
Chain: h.options.Chain,
|
||||
Resolver: h.options.Resolver,
|
||||
Hosts: h.options.Hosts,
|
||||
Logger: h.options.Logger,
|
||||
}
|
||||
h.logger = h.options.Logger
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
@ -65,8 +81,8 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
}).Infof("%s >< %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
}()
|
||||
|
||||
if h.md.cipher != nil {
|
||||
conn = ss.ShadowConn(h.md.cipher.StreamConn(conn), nil)
|
||||
if h.cipher != nil {
|
||||
conn = ss.ShadowConn(h.cipher.StreamConn(conn), nil)
|
||||
}
|
||||
|
||||
if h.md.readTimeout > 0 {
|
||||
@ -86,7 +102,7 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
||||
h.logger.Infof("%s >> %s", conn.RemoteAddr(), addr)
|
||||
|
||||
if h.bypass != nil && h.bypass.Contains(addr.String()) {
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(addr.String()) {
|
||||
h.logger.Info("bypass: ", addr.String())
|
||||
return
|
||||
}
|
||||
|
@ -1,41 +1,23 @@
|
||||
package ss
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/common/util/ss"
|
||||
mdata "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
cipher core.Cipher
|
||||
key string
|
||||
readTimeout time.Duration
|
||||
}
|
||||
|
||||
func (h *ssHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
users = "users"
|
||||
key = "key"
|
||||
readTimeout = "readTimeout"
|
||||
)
|
||||
|
||||
var method, password string
|
||||
if auths := mdata.GetStrings(md, users); len(auths) > 0 {
|
||||
auth := auths[0]
|
||||
ss := strings.SplitN(auth, ":", 2)
|
||||
if len(ss) == 1 {
|
||||
method = ss[0]
|
||||
} else {
|
||||
method, password = ss[0], ss[1]
|
||||
}
|
||||
}
|
||||
h.md.cipher, err = ss.ShadowCipher(method, password, mdata.GetString(md, key))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
h.md.key = mdata.GetString(md, key)
|
||||
h.md.readTimeout = mdata.GetDuration(md, readTimeout)
|
||||
|
||||
return
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/bypass"
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/common/bufpool"
|
||||
"github.com/go-gost/gost/pkg/common/util/socks"
|
||||
@ -14,6 +13,7 @@ import (
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -21,31 +21,48 @@ func init() {
|
||||
}
|
||||
|
||||
type ssuHandler struct {
|
||||
bypass bypass.Bypass
|
||||
router *chain.Router
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
cipher core.Cipher
|
||||
router *chain.Router
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
options handler.Options
|
||||
}
|
||||
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
options := &handler.Options{}
|
||||
options := handler.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &ssuHandler{
|
||||
bypass: options.Bypass,
|
||||
router: options.Router,
|
||||
logger: options.Logger,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ssuHandler) Init(md md.Metadata) (err error) {
|
||||
if err := h.parseMetadata(md); err != nil {
|
||||
return err
|
||||
if err = h.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
if len(h.options.Auths) > 0 {
|
||||
method := h.options.Auths[0].Username()
|
||||
password, _ := h.options.Auths[0].Password()
|
||||
h.cipher, err = ss.ShadowCipher(method, password, h.md.key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
h.router = &chain.Router{
|
||||
Retries: h.options.Retries,
|
||||
Chain: h.options.Chain,
|
||||
Resolver: h.options.Resolver,
|
||||
Hosts: h.options.Hosts,
|
||||
Logger: h.options.Logger,
|
||||
}
|
||||
h.logger = h.options.Logger
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (h *ssuHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
@ -66,14 +83,14 @@ func (h *ssuHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
|
||||
pc, ok := conn.(net.PacketConn)
|
||||
if ok {
|
||||
if h.md.cipher != nil {
|
||||
pc = h.md.cipher.PacketConn(pc)
|
||||
if h.cipher != nil {
|
||||
pc = h.cipher.PacketConn(pc)
|
||||
}
|
||||
// standard UDP relay.
|
||||
pc = ss.UDPServerConn(pc, conn.RemoteAddr(), h.md.bufferSize)
|
||||
} else {
|
||||
if h.md.cipher != nil {
|
||||
conn = ss.ShadowConn(h.md.cipher.StreamConn(conn), nil)
|
||||
if h.cipher != nil {
|
||||
conn = ss.ShadowConn(h.cipher.StreamConn(conn), nil)
|
||||
}
|
||||
// UDP over TCP
|
||||
pc = socks.UDPTunServerConn(conn)
|
||||
@ -116,7 +133,7 @@ func (h *ssuHandler) relayPacket(pc1, pc2 net.PacketConn) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if h.bypass != nil && h.bypass.Contains(addr.String()) {
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(addr.String()) {
|
||||
h.logger.Warn("bypass: ", addr)
|
||||
return nil
|
||||
}
|
||||
@ -148,7 +165,7 @@ func (h *ssuHandler) relayPacket(pc1, pc2 net.PacketConn) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if h.bypass != nil && h.bypass.Contains(raddr.String()) {
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(raddr.String()) {
|
||||
h.logger.Warn("bypass: ", raddr)
|
||||
return nil
|
||||
}
|
||||
|
@ -2,43 +2,25 @@ package ss
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/common/util/ss"
|
||||
mdata "github.com/go-gost/gost/pkg/metadata"
|
||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
cipher core.Cipher
|
||||
key string
|
||||
readTimeout time.Duration
|
||||
bufferSize int
|
||||
}
|
||||
|
||||
func (h *ssuHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
users = "users"
|
||||
key = "key"
|
||||
readTimeout = "readTimeout"
|
||||
bufferSize = "bufferSize"
|
||||
)
|
||||
|
||||
var method, password string
|
||||
if auths := mdata.GetStrings(md, users); len(auths) > 0 {
|
||||
auth := auths[0]
|
||||
ss := strings.SplitN(auth, ":", 2)
|
||||
if len(ss) == 1 {
|
||||
method = ss[0]
|
||||
} else {
|
||||
method, password = ss[0], ss[1]
|
||||
}
|
||||
}
|
||||
h.md.cipher, err = ss.ShadowCipher(method, password, mdata.GetString(md, key))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
h.md.key = mdata.GetString(md, key)
|
||||
h.md.readTimeout = mdata.GetDuration(md, readTimeout)
|
||||
|
||||
if bs := mdata.GetInt(md, bufferSize); bs > 0 {
|
||||
|
Reference in New Issue
Block a user