From cd5f2d691a151caaf838565aea9d71af82fe2603 Mon Sep 17 00:00:00 2001 From: imilo Date: Wed, 21 Dec 2016 10:46:54 +0200 Subject: [PATCH 1/2] New settings option for a custom SSH host (#3763) --- conf/app.ini | 2 ++ modules/setting/setting.go | 1 + modules/ssh/ssh.go | 10 +++++----- routers/init.go | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/conf/app.ini b/conf/app.ini index 18ad9c3d215d..f7b32430b52d 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -103,6 +103,8 @@ DISABLE_SSH = false START_SSH_SERVER = false ; Domain name to be exposed in clone URL SSH_DOMAIN = %(DOMAIN)s +; Network interface builtin SSH server listens on +SSH_LISTEN_HOST = 0.0.0.0 ; Port number to be exposed in clone URL SSH_PORT = 22 ; Port number builtin SSH server listens on diff --git a/modules/setting/setting.go b/modules/setting/setting.go index d78b22c48489..9c00eb7437f6 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -82,6 +82,7 @@ var ( StartBuiltinServer bool `ini:"START_SSH_SERVER"` Domain string `ini:"SSH_DOMAIN"` Port int `ini:"SSH_PORT"` + ListenHost string `ini:"SSH_LISTEN_HOST"` ListenPort int `ini:"SSH_LISTEN_PORT"` RootPath string `ini:"SSH_ROOT_PATH"` KeyTestPath string `ini:"SSH_KEY_TEST_PATH"` diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index b35973cc326b..056ef084efc3 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -110,10 +110,10 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) { } } -func listen(config *ssh.ServerConfig, port int) { - listener, err := net.Listen("tcp", "0.0.0.0:"+com.ToStr(port)) +func listen(config *ssh.ServerConfig, host string, port int) { + listener, err := net.Listen("tcp", host+":"+com.ToStr(port)) if err != nil { - panic(err) + log.Fatal(4, "Fail to start SSH server: %v", err) } for { // Once a ServerConfig has been configured, connections can be accepted. @@ -148,7 +148,7 @@ func listen(config *ssh.ServerConfig, port int) { } // Listen starts a SSH server listens on given port. -func Listen(port int) { +func Listen(host string, port int) { config := &ssh.ServerConfig{ PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key)))) @@ -185,5 +185,5 @@ func Listen(port int) { } config.AddHostKey(private) - go listen(config, port) + go listen(config, host, port) } diff --git a/routers/init.go b/routers/init.go index 3d5235d79d20..697f33835cde 100644 --- a/routers/init.go +++ b/routers/init.go @@ -73,7 +73,7 @@ func GlobalInit() { checkRunMode() if setting.InstallLock && setting.SSH.StartBuiltinServer { - ssh.Listen(setting.SSH.ListenPort) - log.Info("SSH server started on :%v", setting.SSH.ListenPort) + ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort) + log.Info("SSH server started on %s:%v", setting.SSH.ListenHost, setting.SSH.ListenPort) } } From 88b469ea505cc325d5e5757fbefa2824d3738d62 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 22 Dec 2016 17:03:15 +0800 Subject: [PATCH 2/2] let default ssh listen addr empty --- conf/app.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/app.ini b/conf/app.ini index f7b32430b52d..84cb05b27e04 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -104,7 +104,7 @@ START_SSH_SERVER = false ; Domain name to be exposed in clone URL SSH_DOMAIN = %(DOMAIN)s ; Network interface builtin SSH server listens on -SSH_LISTEN_HOST = 0.0.0.0 +SSH_LISTEN_HOST = ; Port number to be exposed in clone URL SSH_PORT = 22 ; Port number builtin SSH server listens on