5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2025-01-11 11:55:41 +00:00
yggdrasil-go/src/core/link_socks.go

43 lines
917 B
Go
Raw Normal View History

package core
import (
2023-04-06 20:45:49 +00:00
"context"
"fmt"
"net"
"net/url"
"strings"
"golang.org/x/net/proxy"
)
type linkSOCKS struct {
*links
}
func (l *links) newLinkSOCKS() *linkSOCKS {
lt := &linkSOCKS{
links: l,
}
return lt
}
2023-04-06 20:45:49 +00:00
func (l *linkSOCKS) dial(url *url.URL, info linkInfo, options linkOptions) (net.Conn, error) {
var proxyAuth *proxy.Auth
if url.User != nil && url.User.Username() != "" {
proxyAuth = &proxy.Auth{
User: url.User.Username(),
}
proxyAuth.Password, _ = url.User.Password()
}
dialer, err := proxy.SOCKS5("tcp", url.Host, proxyAuth, proxy.Direct)
if err != nil {
2023-04-06 20:45:49 +00:00
return nil, fmt.Errorf("failed to configure proxy")
}
pathtokens := strings.Split(strings.Trim(url.Path, "/"), "/")
2023-04-06 20:45:49 +00:00
return dialer.Dial("tcp", pathtokens[0])
}
2023-04-06 20:45:49 +00:00
func (l *linkSOCKS) listen(ctx context.Context, url *url.URL, _ string) (net.Listener, error) {
return nil, fmt.Errorf("SOCKS listener not supported")
}