socks5.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. frpNet "github.com/fatedier/frp/pkg/util/net"
  20. gosocks5 "github.com/armon/go-socks5"
  21. )
  22. const PluginSocks5 = "socks5"
  23. func init() {
  24. Register(PluginSocks5, NewSocks5Plugin)
  25. }
  26. type Socks5Plugin struct {
  27. Server *gosocks5.Server
  28. user string
  29. passwd string
  30. }
  31. func NewSocks5Plugin(params map[string]string) (p Plugin, err error) {
  32. user := params["plugin_user"]
  33. passwd := params["plugin_passwd"]
  34. cfg := &gosocks5.Config{
  35. Logger: log.New(io.Discard, "", log.LstdFlags),
  36. }
  37. if user != "" || passwd != "" {
  38. cfg.Credentials = gosocks5.StaticCredentials(map[string]string{user: passwd})
  39. }
  40. sp := &Socks5Plugin{}
  41. sp.Server, err = gosocks5.New(cfg)
  42. p = sp
  43. return
  44. }
  45. func (sp *Socks5Plugin) Handle(conn io.ReadWriteCloser, realConn net.Conn, extraBufToLocal []byte) {
  46. defer conn.Close()
  47. wrapConn := frpNet.WrapReadWriteCloserToConn(conn, realConn)
  48. sp.Server.ServeConn(wrapConn)
  49. }
  50. func (sp *Socks5Plugin) Name() string {
  51. return PluginSocks5
  52. }
  53. func (sp *Socks5Plugin) Close() error {
  54. return nil
  55. }