Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ func (r HostRewriter) Rewrite(ctx context.Context, request *socks5.Request) (con
return ctx, request.DestAddr
}

// SOCKS5 mapping DNS resolver
type HostResolver struct {
hostsMap map[string]string
}

func (r HostResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
dst, found := r.hostsMap[name]
if found {
return ctx, net.ParseIP(dst), nil
}

addr, err := net.ResolveIPAddr("ip", name)
if err != nil {
return ctx, nil, err
}
return ctx, addr.IP, err
}

func runAction(cmd *cobra.Command, args []string) error {
listenAddress, err := cmd.Flags().GetString("listen-address")
if err != nil {
Expand Down Expand Up @@ -113,6 +131,7 @@ func runAction(cmd *cobra.Command, args []string) error {
proxy := socks5.NewServer(
socks5.WithLogger(logrus.StandardLogger()),
socks5.WithRewriter(HostRewriter{hostsMap: hostsMap}),
socks5.WithResolver(HostResolver{hostsMap: hostsMap}),
)
if err := proxy.ListenAndServe("tcp", listenAddress); err != nil {
logrus.Fatal(err)
Expand Down