| // |
| 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 | package ssh |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net" |
| 20 | "os" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 |
| 24 | "golang.org/x/crypto/ssh" |
| 25 |
| 26 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 27 | "github.com/fatedier/frp/pkg/transport" |
| 28 | "github.com/fatedier/frp/pkg/util/log" |
| 29 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 30 | ) |
| 31 |
| 32 | type Gateway struct { |
| 33 | bindPort int |
| 34 | ln net.Listener |
| 35 |
| 36 | peerServerListener *netpkg.InternalListener |
| 37 |
| 38 | sshConfig *ssh.ServerConfig |
| 39 | } |
| 40 |
| 41 | func NewGateway( |
| 42 | cfg v1.SSHTunnelGateway, bindAddr string, |
| 43 | peerServerListener *netpkg.InternalListener, |
| 44 | ) (*Gateway, error) { |
| 45 | sshConfig := &ssh.ServerConfig{} |
| 46 |
| 47 | // privateKey |
| 48 | var ( |
| 49 | privateKeyBytes []byte |
| 50 | err error |
| 51 | ) |
| 52 | if cfg.PrivateKeyFile != "" { |
| 53 | privateKeyBytes, err = os.ReadFile(cfg.PrivateKeyFile) |
| 54 | } else { |
| 55 | if cfg.AutoGenPrivateKeyPath != "" { |
| 56 | privateKeyBytes, _ = os.ReadFile(cfg.AutoGenPrivateKeyPath) |
| 57 | } |
| 58 | if len(privateKeyBytes) == 0 { |
| 59 | privateKeyBytes, err = transport.NewRandomPrivateKey() |
| 60 | if err == nil && cfg.AutoGenPrivateKeyPath != "" { |
| 61 | err = os.WriteFile(cfg.AutoGenPrivateKeyPath, privateKeyBytes, 0o600) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | privateKey, err := ssh.ParsePrivateKey(privateKeyBytes) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | sshConfig.AddHostKey(privateKey) |
| 73 |
| 74 | sshConfig.NoClientAuth = cfg.AuthorizedKeysFile == "" |
| 75 | sshConfig.PublicKeyCallback = func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { |
| 76 | authorizedKeysMap, err := loadAuthorizedKeysFromFile(cfg.AuthorizedKeysFile) |
| 77 | if err != nil { |
| 78 | log.Errorf("load authorized keys file error: %v", err) |
| 79 | return nil, fmt.Errorf("internal error") |
| 80 | } |
| 81 |
| 82 | user, ok := authorizedKeysMap[string(key.Marshal())] |
| 83 | if !ok { |
| 84 | return nil, fmt.Errorf("unknown public key for remoteAddr %q", conn.RemoteAddr()) |
| 85 | } |
| 86 | return &ssh.Permissions{ |
| 87 | Extensions: map[string]string{ |
| 88 | "user": user, |
| 89 | }, |
| 90 | }, nil |
| 91 | } |
| 92 |
| 93 | ln, err := net.Listen("tcp", net.JoinHostPort(bindAddr, strconv.Itoa(cfg.BindPort))) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | return &Gateway{ |
| 98 | bindPort: cfg.BindPort, |
| 99 | ln: ln, |
| 100 | peerServerListener: peerServerListener, |
| 101 | sshConfig: sshConfig, |
| 102 | }, nil |
| 103 | } |
| 104 |
| 105 | func (g *Gateway) Run() { |
| 106 | for { |
| 107 | conn, err := g.ln.Accept() |
| 108 | if err != nil { |
| 109 | return |
| 110 | } |
| 111 | go g.handleConn(conn) |
| 112 | } |
| 113 | } |
| 114 |
| 115 | func (g *Gateway) Close() error { |
| 116 | return g.ln.Close() |
| 117 | } |
| 118 |
| 119 | func (g *Gateway) handleConn(conn net.Conn) { |
| 120 | defer conn.Close() |
| 121 |
| 122 | ts, err := NewTunnelServer(conn, g.sshConfig, g.peerServerListener) |
| 123 | if err != nil { |
| 124 | return |
| 125 | } |
| 126 | if err := ts.Run(); err != nil { |
| 127 | log.Errorf("ssh tunnel server run error: %v", err) |
| 128 | } |
| 129 | } |
| 130 |
| 131 | func loadAuthorizedKeysFromFile(path string) (map[string]string, error) { |
| 132 | authorizedKeysMap := make(map[string]string) // value is username |
| 133 | authorizedKeysBytes, err := os.ReadFile(path) |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | for len(authorizedKeysBytes) > 0 { |
| 138 | pubKey, comment, _, rest, err := ssh.ParseAuthorizedKey(authorizedKeysBytes) |
| 139 | if err != nil { |
| 140 | return nil, err |
| 141 | } |
| 142 |
| 143 | authorizedKeysMap[string(pubKey.Marshal())] = strings.TrimSpace(comment) |
| 144 | authorizedKeysBytes = rest |
| 145 | } |
| 146 | return authorizedKeysMap, nil |
| 147 | } |