Skip to content

Commit 65b1875

Browse files
lunnytboerger
authored andcommitted
New settings option for a custom SSH host (#3763) (#446)
* New settings option for a custom SSH host (#3763) * let default ssh listen addr empty
1 parent 11df7eb commit 65b1875

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

conf/app.ini

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ DISABLE_SSH = false
103103
START_SSH_SERVER = false
104104
; Domain name to be exposed in clone URL
105105
SSH_DOMAIN = %(DOMAIN)s
106+
; Network interface builtin SSH server listens on
107+
SSH_LISTEN_HOST =
106108
; Port number to be exposed in clone URL
107109
SSH_PORT = 22
108110
; Port number builtin SSH server listens on

modules/setting/setting.go

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ var (
8282
StartBuiltinServer bool `ini:"START_SSH_SERVER"`
8383
Domain string `ini:"SSH_DOMAIN"`
8484
Port int `ini:"SSH_PORT"`
85+
ListenHost string `ini:"SSH_LISTEN_HOST"`
8586
ListenPort int `ini:"SSH_LISTEN_PORT"`
8687
RootPath string `ini:"SSH_ROOT_PATH"`
8788
KeyTestPath string `ini:"SSH_KEY_TEST_PATH"`

modules/ssh/ssh.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) {
110110
}
111111
}
112112

113-
func listen(config *ssh.ServerConfig, port int) {
114-
listener, err := net.Listen("tcp", "0.0.0.0:"+com.ToStr(port))
113+
func listen(config *ssh.ServerConfig, host string, port int) {
114+
listener, err := net.Listen("tcp", host+":"+com.ToStr(port))
115115
if err != nil {
116-
panic(err)
116+
log.Fatal(4, "Fail to start SSH server: %v", err)
117117
}
118118
for {
119119
// Once a ServerConfig has been configured, connections can be accepted.
@@ -148,7 +148,7 @@ func listen(config *ssh.ServerConfig, port int) {
148148
}
149149

150150
// Listen starts a SSH server listens on given port.
151-
func Listen(port int) {
151+
func Listen(host string, port int) {
152152
config := &ssh.ServerConfig{
153153
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
154154
pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))))
@@ -185,5 +185,5 @@ func Listen(port int) {
185185
}
186186
config.AddHostKey(private)
187187

188-
go listen(config, port)
188+
go listen(config, host, port)
189189
}

routers/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func GlobalInit() {
7373
checkRunMode()
7474

7575
if setting.InstallLock && setting.SSH.StartBuiltinServer {
76-
ssh.Listen(setting.SSH.ListenPort)
77-
log.Info("SSH server started on :%v", setting.SSH.ListenPort)
76+
ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort)
77+
log.Info("SSH server started on %s:%v", setting.SSH.ListenHost, setting.SSH.ListenPort)
7878
}
7979
}

0 commit comments

Comments
 (0)