MCPcopy Index your code
hub / github.com/1Panel-dev/1Panel / updateSSHSocketFile

Function updateSSHSocketFile

agent/app/service/ssh.go:1604–1639  ·  view source on GitHub ↗
(newPort string)

Source from the content-addressed store, hash-verified

1602}
1603
1604func updateSSHSocketFile(newPort string) error {
1605 active, _ := controller.CheckActive("ssh.socket")
1606 if !active {
1607 return nil
1608 }
1609 filepath := "/usr/lib/systemd/system/ssh.socket"
1610 file, err := os.ReadFile(filepath)
1611 if err != nil {
1612 return err
1613 }
1614 lines := strings.Split(string(file), "\n")
1615 for i := 0; i < len(lines); i++ {
1616 if strings.HasPrefix(lines[i], "ListenStream=") {
1617 parts := strings.Split(lines[i], ":")
1618 if len(parts) > 1 {
1619 lines[i] = strings.ReplaceAll(lines[i], parts[len(parts)-1], newPort)
1620 continue
1621 }
1622 parts = strings.Split(lines[i], "=")
1623 if len(parts) > 1 {
1624 lines[i] = strings.ReplaceAll(lines[i], parts[len(parts)-1], newPort)
1625 }
1626 }
1627 }
1628 fileItem, err := os.OpenFile(filepath, os.O_WRONLY|os.O_TRUNC, constant.FilePerm)
1629 if err != nil {
1630 return err
1631 }
1632 defer fileItem.Close()
1633 if _, err = fileItem.WriteString(strings.Join(lines, "\n")); err != nil {
1634 return err
1635 }
1636 _ = controller.Reload()
1637 _ = controller.HandleRestart("ssh.socket")
1638 return nil
1639}
1640
1641func loadSSHPort() string {
1642 port := "22"

Callers 1

handleSSHPortUpdateFunction · 0.85

Calls 3

OpenFileMethod · 0.80
CloseMethod · 0.65
ReloadMethod · 0.65

Tested by

no test coverage detected