socks5.go 1.6 KB

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