1
0

socks5.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. //go:build !frps
  15. package plugin
  16. import (
  17. "io"
  18. "log"
  19. "net"
  20. gosocks5 "github.com/armon/go-socks5"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. netpkg "github.com/fatedier/frp/pkg/util/net"
  23. )
  24. func init() {
  25. Register(v1.PluginSocks5, NewSocks5Plugin)
  26. }
  27. type Socks5Plugin struct {
  28. Server *gosocks5.Server
  29. }
  30. func NewSocks5Plugin(options v1.ClientPluginOptions) (p Plugin, err error) {
  31. opts := options.(*v1.Socks5PluginOptions)
  32. cfg := &gosocks5.Config{
  33. Logger: log.New(io.Discard, "", log.LstdFlags),
  34. }
  35. if opts.Username != "" || opts.Password != "" {
  36. cfg.Credentials = gosocks5.StaticCredentials(map[string]string{opts.Username: opts.Password})
  37. }
  38. sp := &Socks5Plugin{}
  39. sp.Server, err = gosocks5.New(cfg)
  40. p = sp
  41. return
  42. }
  43. func (sp *Socks5Plugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, _ *ExtraInfo) {
  44. defer conn.Close()
  45. wrapConn := netpkg.WrapReadWriteCloserToConn(conn, realConn)
  46. _ = sp.Server.ServeConn(wrapConn)
  47. }
  48. func (sp *Socks5Plugin) Name() string {
  49. return v1.PluginSocks5
  50. }
  51. func (sp *Socks5Plugin) Close() error {
  52. return nil
  53. }