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

Function initLocalFile

agent/utils/toolbox/fail2ban.go:141–189  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

139}
140
141func initLocalFile() error {
142 f, err := os.Create(defaultPath)
143 if err != nil {
144 return err
145 }
146 defer f.Close()
147 initFile := `#DEFAULT-START
148[DEFAULT]
149bantime = 600
150findtime = 300
151maxretry = 5
152banaction = $banaction
153action = %(action_mwl)s
154#DEFAULT-END
155
156[sshd]
157ignoreip = 127.0.0.1/8
158enabled = true
159filter = sshd
160port = 22
161maxretry = 5
162findtime = 300
163bantime = 600
164banaction = $banaction
165action = %(action_mwl)s
166logpath = $logpath`
167
168 banaction := ""
169 if active, _ := controller.CheckActive("firewalld"); active {
170 banaction = "firewallcmd-ipset"
171 } else if active, _ := controller.CheckActive("ufw"); active {
172 banaction = "ufw"
173 } else {
174 banaction = "iptables-allports"
175 }
176 initFile = strings.ReplaceAll(initFile, "$banaction", banaction)
177
178 logPath := ""
179 if _, err := os.Stat("/var/log/secure"); err == nil {
180 logPath = "/var/log/secure"
181 } else {
182 logPath = "/var/log/auth.log"
183 }
184 initFile = strings.ReplaceAll(initFile, "$logpath", logPath)
185 if err := os.WriteFile(defaultPath, []byte(initFile), 0640); err != nil {
186 return err
187 }
188 return nil
189}

Callers 1

NewFail2BanFunction · 0.85

Calls 4

WriteFileMethod · 0.80
CreateMethod · 0.65
CloseMethod · 0.65
StatMethod · 0.45

Tested by

no test coverage detected