MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / loadSSHConnByProxy

Function loadSSHConnByProxy

core/utils/ssh/ssh.go:271–321  ·  view source on GitHub ↗
(network, addr string, timeout time.Duration)

Source from the content-addressed store, hash-verified

269}
270
271func loadSSHConnByProxy(network, addr string, timeout time.Duration) (net.Conn, error) {
272 settingRepo := repo.NewISettingRepo()
273 proxyType, err := settingRepo.Get(repo.WithByKey("ProxyType"))
274 if err != nil {
275 return nil, fmt.Errorf("get proxy type from db failed, err: %v", err)
276 }
277 if len(proxyType.Value) == 0 {
278 return nil, fmt.Errorf("get proxy type from db failed, err: %v", err)
279 }
280 proxyUrl, _ := settingRepo.Get(repo.WithByKey("ProxyUrl"))
281 port, _ := settingRepo.Get(repo.WithByKey("ProxyPort"))
282 user, _ := settingRepo.Get(repo.WithByKey("ProxyUser"))
283 passwd, _ := settingRepo.Get(repo.WithByKey("ProxyPasswd"))
284
285 pass, _ := encrypt.StringDecrypt(passwd.Value)
286 proxyItem := fmt.Sprintf("%s:%s", proxyUrl.Value, port.Value)
287 switch proxyType.Value {
288 case "http", "https":
289 item := HTTPProxyDialer{
290 Type: proxyType.Value,
291 URL: proxyItem,
292 User: user.Value,
293 Password: pass,
294 }
295 return HTTPDial(item, network, addr)
296 case "socks5":
297 var auth *proxy.Auth
298 if len(user.Value) == 0 {
299 auth = nil
300 } else {
301 auth = &proxy.Auth{
302 User: user.Value,
303 Password: pass,
304 }
305 }
306 dialer, err := proxy.SOCKS5("tcp", proxyItem, auth, &net.Dialer{
307 Timeout: 30 * time.Second,
308 KeepAlive: 30 * time.Second,
309 })
310 if err != nil {
311 return nil, fmt.Errorf("new socks5 proxy failed, err: %v", err)
312 }
313 return dialer.Dial(network, addr)
314 default:
315 conn, err := net.DialTimeout(network, addr, timeout)
316 if err != nil {
317 return nil, err
318 }
319 return conn, nil
320 }
321}

Callers 1

DialWithTimeoutFunction · 0.85

Calls 3

GetMethod · 0.95
HTTPDialFunction · 0.85
WithByKeyMethod · 0.65

Tested by

no test coverage detected