2020-05-12 11:29:53 +08:00
|
|
|
package mixed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
2024-08-25 19:26:06 +08:00
|
|
|
"github.com/metacubex/mihomo/component/auth"
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2024-07-25 19:49:56 +08:00
|
|
|
authStore "github.com/metacubex/mihomo/listener/auth"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/listener/http"
|
|
|
|
"github.com/metacubex/mihomo/listener/socks"
|
|
|
|
"github.com/metacubex/mihomo/transport/socks4"
|
|
|
|
"github.com/metacubex/mihomo/transport/socks5"
|
2020-05-12 11:29:53 +08:00
|
|
|
)
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
type Listener struct {
|
2022-12-05 00:20:50 +08:00
|
|
|
listener net.Listener
|
|
|
|
addr string
|
|
|
|
closed bool
|
2021-08-01 00:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RawAddress implements C.Listener
|
|
|
|
func (l *Listener) RawAddress() string {
|
|
|
|
return l.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements C.Listener
|
|
|
|
func (l *Listener) Address() string {
|
|
|
|
return l.listener.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements C.Listener
|
|
|
|
func (l *Listener) Close() error {
|
|
|
|
l.closed = true
|
|
|
|
return l.listener.Close()
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func New(addr string, tunnel C.Tunnel, additions ...inbound.Addition) (*Listener, error) {
|
2024-09-27 18:10:05 +08:00
|
|
|
return NewWithAuthenticator(addr, tunnel, authStore.Default, additions...)
|
2024-08-25 19:26:06 +08:00
|
|
|
}
|
|
|
|
|
2024-09-27 18:10:05 +08:00
|
|
|
func NewWithAuthenticator(addr string, tunnel C.Tunnel, store auth.AuthStore, additions ...inbound.Addition) (*Listener, error) {
|
2024-02-07 18:22:54 +08:00
|
|
|
isDefault := false
|
2022-12-05 00:20:50 +08:00
|
|
|
if len(additions) == 0 {
|
2024-02-07 18:22:54 +08:00
|
|
|
isDefault = true
|
2022-12-05 10:12:53 +08:00
|
|
|
additions = []inbound.Addition{
|
|
|
|
inbound.WithInName("DEFAULT-MIXED"),
|
|
|
|
inbound.WithSpecialRules(""),
|
|
|
|
}
|
2022-12-05 00:20:50 +08:00
|
|
|
}
|
2024-09-24 21:42:28 +08:00
|
|
|
|
2022-11-16 10:43:16 +08:00
|
|
|
l, err := inbound.Listen("tcp", addr)
|
2020-05-12 11:29:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:07:51 +08:00
|
|
|
ml := &Listener{
|
2022-12-05 00:20:50 +08:00
|
|
|
listener: l,
|
|
|
|
addr: addr,
|
2021-07-19 14:07:51 +08:00
|
|
|
}
|
2020-05-12 11:29:53 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2021-06-13 23:05:22 +08:00
|
|
|
c, err := ml.listener.Accept()
|
2020-05-12 11:29:53 +08:00
|
|
|
if err != nil {
|
|
|
|
if ml.closed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2024-09-27 18:10:05 +08:00
|
|
|
store := store
|
|
|
|
if isDefault || store == authStore.Default { // only apply on default listener
|
2024-02-05 22:40:06 +08:00
|
|
|
if !inbound.IsRemoteAddrDisAllowed(c.RemoteAddr()) {
|
2023-12-12 04:39:11 -08:00
|
|
|
_ = c.Close()
|
|
|
|
continue
|
|
|
|
}
|
2024-08-25 19:26:06 +08:00
|
|
|
if inbound.SkipAuthRemoteAddr(c.RemoteAddr()) {
|
2024-09-27 18:10:05 +08:00
|
|
|
store = authStore.Nil
|
2024-08-25 19:26:06 +08:00
|
|
|
}
|
2023-12-12 04:39:11 -08:00
|
|
|
}
|
2024-09-27 18:10:05 +08:00
|
|
|
go handleConn(c, tunnel, store, additions...)
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ml, nil
|
|
|
|
}
|
|
|
|
|
2024-09-27 18:10:05 +08:00
|
|
|
func handleConn(conn net.Conn, tunnel C.Tunnel, store auth.AuthStore, additions ...inbound.Addition) {
|
2021-06-15 17:13:40 +08:00
|
|
|
bufConn := N.NewBufferedConn(conn)
|
2020-05-12 11:29:53 +08:00
|
|
|
head, err := bufConn.Peek(1)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-18 16:09:09 +08:00
|
|
|
switch head[0] {
|
|
|
|
case socks4.Version:
|
2024-09-27 18:10:05 +08:00
|
|
|
socks.HandleSocks4(bufConn, tunnel, store, additions...)
|
2021-07-18 16:09:09 +08:00
|
|
|
case socks5.Version:
|
2024-09-27 18:10:05 +08:00
|
|
|
socks.HandleSocks5(bufConn, tunnel, store, additions...)
|
2021-07-18 16:09:09 +08:00
|
|
|
default:
|
2024-09-27 18:10:05 +08:00
|
|
|
http.HandleConn(bufConn, tunnel, store, additions...)
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
}
|