2017-07-01 15:56:48 +08:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
2019-10-12 20:13:12 +08:00
|
|
|
"net"
|
2017-07-01 15:56:48 +08:00
|
|
|
|
|
|
|
gosocks5 "github.com/armon/go-socks5"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-05-29 14:10:34 +08:00
|
|
|
utilnet "github.com/fatedier/frp/pkg/util/net"
|
2017-07-01 15:56:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-09-06 10:18:02 +08:00
|
|
|
Register(v1.PluginSocks5, NewSocks5Plugin)
|
2017-07-01 15:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Socks5Plugin struct {
|
|
|
|
Server *gosocks5.Server
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func NewSocks5Plugin(options v1.ClientPluginOptions) (p Plugin, err error) {
|
|
|
|
opts := options.(*v1.Socks5PluginOptions)
|
2018-01-24 23:04:55 +08:00
|
|
|
|
|
|
|
cfg := &gosocks5.Config{
|
2021-09-29 10:33:57 +08:00
|
|
|
Logger: log.New(io.Discard, "", log.LstdFlags),
|
2018-01-24 23:04:55 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
if opts.Username != "" || opts.Password != "" {
|
|
|
|
cfg.Credentials = gosocks5.StaticCredentials(map[string]string{opts.Username: opts.Password})
|
2018-01-24 23:04:55 +08:00
|
|
|
}
|
|
|
|
sp := &Socks5Plugin{}
|
|
|
|
sp.Server, err = gosocks5.New(cfg)
|
2017-07-01 15:56:48 +08:00
|
|
|
p = sp
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-20 15:18:50 +08:00
|
|
|
func (sp *Socks5Plugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
|
2017-07-01 15:56:48 +08:00
|
|
|
defer conn.Close()
|
2023-05-29 14:10:34 +08:00
|
|
|
wrapConn := utilnet.WrapReadWriteCloserToConn(conn, realConn)
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = sp.Server.ServeConn(wrapConn)
|
2017-07-01 15:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *Socks5Plugin) Name() string {
|
2023-09-06 10:18:02 +08:00
|
|
|
return v1.PluginSocks5
|
2017-07-01 15:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *Socks5Plugin) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|