| 1 | // Copyright 2017 fatedier, fatedier@gmail.com |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //go:build !frps |
| 16 | |
| 17 | package client |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "io" |
| 22 | "log" |
| 23 | |
| 24 | gosocks5 "github.com/armon/go-socks5" |
| 25 | |
| 26 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 27 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 28 | ) |
| 29 | |
| 30 | func init() { |
| 31 | Register(v1.PluginSocks5, NewSocks5Plugin) |
| 32 | } |
| 33 | |
| 34 | type Socks5Plugin struct { |
| 35 | Server *gosocks5.Server |
| 36 | } |
| 37 | |
| 38 | func NewSocks5Plugin(_ PluginContext, options v1.ClientPluginOptions) (p Plugin, err error) { |
| 39 | opts := options.(*v1.Socks5PluginOptions) |
| 40 | |
| 41 | cfg := &gosocks5.Config{ |
| 42 | Logger: log.New(io.Discard, "", log.LstdFlags), |
| 43 | } |
| 44 | if opts.Username != "" || opts.Password != "" { |
| 45 | cfg.Credentials = gosocks5.StaticCredentials(map[string]string{opts.Username: opts.Password}) |
| 46 | } |
| 47 | sp := &Socks5Plugin{} |
| 48 | sp.Server, err = gosocks5.New(cfg) |
| 49 | p = sp |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | func (sp *Socks5Plugin) Handle(_ context.Context, connInfo *ConnectionInfo) { |
| 54 | defer connInfo.Conn.Close() |
| 55 | wrapConn := netpkg.WrapReadWriteCloserToConn(connInfo.Conn, connInfo.UnderlyingConn) |
| 56 | _ = sp.Server.ServeConn(wrapConn) |
| 57 | } |
| 58 | |
| 59 | func (sp *Socks5Plugin) Name() string { |
| 60 | return v1.PluginSocks5 |
| 61 | } |
| 62 | |
| 63 | func (sp *Socks5Plugin) Close() error { |
| 64 | return nil |
| 65 | } |