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

Method UpdateByFile

agent/app/service/ssh.go:787–822  ·  view source on GitHub ↗
(req dto.SSHConfUpdate)

Source from the content-addressed store, hash-verified

785}
786
787func (u *SSHService) UpdateByFile(req dto.SSHConfUpdate) error {
788 var fileName string
789 switch req.Key {
790 case "authKeys":
791 currentUser, err := user.Current()
792 if err != nil {
793 return fmt.Errorf("load current user failed, err: %v", err)
794 }
795 fileName = currentUser.HomeDir + "/.ssh/authorized_keys"
796 case "sshdConf":
797 fileName = "/etc/ssh/sshd_config"
798 case "sshdConfPath":
799 fileName = req.Path
800 if !isSSHConfigPathAllowed(fileName) {
801 return buserr.WithName("ErrNotSupportType", req.Key)
802 }
803 default:
804 return buserr.WithName("ErrNotSupportType", req.Key)
805 }
806 file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC, constant.FilePerm)
807 if err != nil {
808 return err
809 }
810 defer file.Close()
811 if _, err = file.WriteString(req.Value); err != nil {
812 return err
813 }
814 if req.Key == "authKeys" {
815 return nil
816 }
817 serviceName, err := loadServiceName()
818 if err != nil {
819 return err
820 }
821 return restartSSHService(serviceName)
822}
823
824func sortFileList(fileNames []sshFileItem) []sshFileItem {
825 if len(fileNames) < 2 {

Callers

nothing calls this directly

Calls 5

isSSHConfigPathAllowedFunction · 0.85
loadServiceNameFunction · 0.85
restartSSHServiceFunction · 0.85
OpenFileMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected