2023-02-28 22:54:29 +03:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
2023-04-13 22:37:13 +03:00
|
|
|
"encoding/json"
|
2023-06-24 23:36:18 +03:00
|
|
|
"log"
|
2023-04-13 22:37:13 +03:00
|
|
|
"os"
|
2023-09-01 12:53:50 +03:00
|
|
|
"os/exec"
|
2023-04-13 22:37:13 +03:00
|
|
|
"regexp"
|
2023-07-01 15:26:43 +03:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
"x-ui/database"
|
|
|
|
"x-ui/database/model"
|
2023-04-13 22:37:13 +03:00
|
|
|
"x-ui/logger"
|
|
|
|
"x-ui/xray"
|
2023-02-28 22:54:29 +03:00
|
|
|
)
|
|
|
|
|
2023-08-09 00:07:05 +03:00
|
|
|
type CheckClientIpJob struct{}
|
2023-04-13 22:37:13 +03:00
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
var job *CheckClientIpJob
|
2023-06-03 18:29:32 +03:00
|
|
|
var disAllowedIps []string
|
2023-07-01 15:26:43 +03:00
|
|
|
var ipFiles = []string{
|
|
|
|
xray.GetIPLimitLogPath(),
|
|
|
|
xray.GetIPLimitBannedLogPath(),
|
|
|
|
xray.GetAccessPersistentLogPath(),
|
|
|
|
}
|
2023-02-28 22:54:29 +03:00
|
|
|
|
|
|
|
func NewCheckClientIpJob() *CheckClientIpJob {
|
2023-06-03 18:29:32 +03:00
|
|
|
job = new(CheckClientIpJob)
|
2023-02-28 22:54:29 +03:00
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *CheckClientIpJob) Run() {
|
2023-06-16 00:38:35 +03:00
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
// create files required for iplimit if not exists
|
|
|
|
for i := 0; i < len(ipFiles); i++ {
|
|
|
|
file, err := os.OpenFile(ipFiles[i], os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
|
|
|
|
j.checkError(err)
|
|
|
|
defer file.Close()
|
|
|
|
}
|
2023-06-24 23:36:18 +03:00
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
// check for limit ip
|
|
|
|
if j.hasLimitIp() {
|
2023-09-01 12:53:50 +03:00
|
|
|
j.checkFail2BanInstalled()
|
2023-07-01 15:26:43 +03:00
|
|
|
j.processLogFile()
|
2023-06-16 00:38:35 +03:00
|
|
|
}
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
func (j *CheckClientIpJob) hasLimitIp() bool {
|
2023-06-16 00:38:35 +03:00
|
|
|
db := database.GetDB()
|
|
|
|
var inbounds []*model.Inbound
|
2023-07-01 15:26:43 +03:00
|
|
|
|
2023-06-16 00:38:35 +03:00
|
|
|
err := db.Model(model.Inbound{}).Find(&inbounds).Error
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, inbound := range inbounds {
|
|
|
|
if inbound.Settings == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
settings := map[string][]model.Client{}
|
|
|
|
json.Unmarshal([]byte(inbound.Settings), &settings)
|
|
|
|
clients := settings["clients"]
|
|
|
|
|
|
|
|
for _, client := range clients {
|
|
|
|
limitIp := client.LimitIP
|
|
|
|
if limitIp > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
2023-06-16 00:38:35 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-01 12:53:50 +03:00
|
|
|
func (j *CheckClientIpJob) checkFail2BanInstalled() {
|
|
|
|
cmd := "fail2ban-client"
|
|
|
|
args := []string{"-h"}
|
|
|
|
|
|
|
|
err := exec.Command(cmd, args...).Run()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warning("fail2ban is not installed. IP limiting may not work properly.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
func (j *CheckClientIpJob) processLogFile() {
|
|
|
|
accessLogPath := xray.GetAccessLogPath()
|
2023-04-13 22:37:13 +03:00
|
|
|
if accessLogPath == "" {
|
2023-05-23 17:24:15 +03:00
|
|
|
logger.Warning("access.log doesn't exist in your config.json")
|
2023-02-28 22:54:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-13 22:37:13 +03:00
|
|
|
data, err := os.ReadFile(accessLogPath)
|
2023-02-28 22:54:29 +03:00
|
|
|
InboundClientIps := make(map[string][]string)
|
2023-07-01 15:26:43 +03:00
|
|
|
j.checkError(err)
|
2023-02-28 22:54:29 +03:00
|
|
|
|
2023-04-28 00:00:49 +03:00
|
|
|
lines := strings.Split(string(data), "\n")
|
2023-02-28 22:54:29 +03:00
|
|
|
for _, line := range lines {
|
2023-06-03 18:29:32 +03:00
|
|
|
ipRegx, _ := regexp.Compile(`[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+`)
|
|
|
|
emailRegx, _ := regexp.Compile(`email:.+`)
|
2023-02-28 22:54:29 +03:00
|
|
|
|
|
|
|
matchesIp := ipRegx.FindString(line)
|
2023-04-13 22:37:13 +03:00
|
|
|
if len(matchesIp) > 0 {
|
2023-02-28 22:54:29 +03:00
|
|
|
ip := string(matchesIp)
|
2023-04-13 22:37:13 +03:00
|
|
|
if ip == "127.0.0.1" || ip == "1.1.1.1" {
|
2023-02-28 22:54:29 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
matchesEmail := emailRegx.FindString(line)
|
2023-04-13 22:37:13 +03:00
|
|
|
if matchesEmail == "" {
|
2023-02-28 22:54:29 +03:00
|
|
|
continue
|
|
|
|
}
|
2023-05-23 17:24:15 +03:00
|
|
|
matchesEmail = strings.TrimSpace(strings.Split(matchesEmail, "email: ")[1])
|
2023-04-13 22:37:13 +03:00
|
|
|
|
2023-06-03 18:29:32 +03:00
|
|
|
if InboundClientIps[matchesEmail] != nil {
|
2023-07-01 15:26:43 +03:00
|
|
|
if j.contains(InboundClientIps[matchesEmail], ip) {
|
2023-06-03 18:29:32 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
InboundClientIps[matchesEmail] = append(InboundClientIps[matchesEmail], ip)
|
|
|
|
|
|
|
|
} else {
|
2023-04-13 22:37:13 +03:00
|
|
|
InboundClientIps[matchesEmail] = append(InboundClientIps[matchesEmail], ip)
|
|
|
|
}
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
2023-06-03 18:29:32 +03:00
|
|
|
disAllowedIps = []string{}
|
2023-06-15 22:15:34 +03:00
|
|
|
shouldCleanLog := false
|
2023-02-28 22:54:29 +03:00
|
|
|
|
|
|
|
for clientEmail, ips := range InboundClientIps {
|
2023-07-01 15:26:43 +03:00
|
|
|
inboundClientIps, err := j.getInboundClientIps(clientEmail)
|
2023-04-28 00:00:49 +03:00
|
|
|
sort.Strings(ips)
|
2023-04-13 22:37:13 +03:00
|
|
|
if err != nil {
|
2023-07-01 15:26:43 +03:00
|
|
|
j.addInboundClientIps(clientEmail, ips)
|
2023-04-13 22:37:13 +03:00
|
|
|
} else {
|
2023-07-01 15:26:43 +03:00
|
|
|
shouldCleanLog = j.updateInboundClientIps(inboundClientIps, clientEmail, ips)
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
2023-05-23 17:24:15 +03:00
|
|
|
|
2023-04-13 22:37:13 +03:00
|
|
|
}
|
2023-06-16 00:38:35 +03:00
|
|
|
|
2023-08-01 23:58:16 +03:00
|
|
|
// added delay before cleaning logs to reduce chance of logging IP that already has been banned
|
|
|
|
time.Sleep(time.Second * 2)
|
2023-07-01 15:26:43 +03:00
|
|
|
|
2023-06-15 22:15:34 +03:00
|
|
|
if shouldCleanLog {
|
2023-07-01 15:26:43 +03:00
|
|
|
// copy access log to persistent file
|
|
|
|
logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
|
|
|
|
j.checkError(err)
|
2023-06-24 23:36:18 +03:00
|
|
|
input, err := os.ReadFile(accessLogPath)
|
2023-07-01 15:26:43 +03:00
|
|
|
j.checkError(err)
|
2023-06-24 23:36:18 +03:00
|
|
|
if _, err := logAccessP.Write(input); err != nil {
|
2023-07-01 15:26:43 +03:00
|
|
|
j.checkError(err)
|
2023-06-24 23:36:18 +03:00
|
|
|
}
|
|
|
|
defer logAccessP.Close()
|
2023-04-13 22:37:13 +03:00
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
// clean access log
|
|
|
|
if err := os.Truncate(xray.GetAccessLogPath(), 0); err != nil {
|
|
|
|
j.checkError(err)
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
|
|
|
func (j *CheckClientIpJob) checkError(e error) {
|
2023-04-13 22:37:13 +03:00
|
|
|
if e != nil {
|
2023-02-28 22:54:29 +03:00
|
|
|
logger.Warning("client ip job err:", e)
|
|
|
|
}
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
|
|
|
func (j *CheckClientIpJob) contains(s []string, str string) bool {
|
2023-02-28 22:54:29 +03:00
|
|
|
for _, v := range s {
|
|
|
|
if v == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-06-03 18:29:32 +03:00
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
return false
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
|
|
|
func (j *CheckClientIpJob) getInboundClientIps(clientEmail string) (*model.InboundClientIps, error) {
|
2023-02-28 22:54:29 +03:00
|
|
|
db := database.GetDB()
|
|
|
|
InboundClientIps := &model.InboundClientIps{}
|
|
|
|
err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return InboundClientIps, nil
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
|
|
|
func (j *CheckClientIpJob) addInboundClientIps(clientEmail string, ips []string) error {
|
2023-02-28 22:54:29 +03:00
|
|
|
inboundClientIps := &model.InboundClientIps{}
|
2023-03-25 19:35:46 +03:00
|
|
|
jsonIps, err := json.Marshal(ips)
|
2023-07-01 15:26:43 +03:00
|
|
|
j.checkError(err)
|
2023-02-28 22:54:29 +03:00
|
|
|
|
|
|
|
inboundClientIps.ClientEmail = clientEmail
|
|
|
|
inboundClientIps.Ips = string(jsonIps)
|
|
|
|
|
|
|
|
db := database.GetDB()
|
|
|
|
tx := db.Begin()
|
|
|
|
|
|
|
|
defer func() {
|
2023-06-03 18:29:32 +03:00
|
|
|
if err == nil {
|
2023-05-25 02:51:31 +03:00
|
|
|
tx.Commit()
|
2023-06-03 18:29:32 +03:00
|
|
|
} else {
|
|
|
|
tx.Rollback()
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = tx.Save(inboundClientIps).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-03 18:29:32 +03:00
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
func (j *CheckClientIpJob) updateInboundClientIps(inboundClientIps *model.InboundClientIps, clientEmail string, ips []string) bool {
|
2023-06-03 18:29:32 +03:00
|
|
|
jsonIps, err := json.Marshal(ips)
|
2023-07-01 15:26:43 +03:00
|
|
|
j.checkError(err)
|
2023-06-03 18:29:32 +03:00
|
|
|
|
|
|
|
inboundClientIps.ClientEmail = clientEmail
|
|
|
|
inboundClientIps.Ips = string(jsonIps)
|
2023-04-13 22:37:13 +03:00
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
// check inbound limitation
|
2023-07-01 15:26:43 +03:00
|
|
|
inbound, err := j.getInboundByEmail(clientEmail)
|
|
|
|
j.checkError(err)
|
2023-02-28 22:54:29 +03:00
|
|
|
|
|
|
|
if inbound.Settings == "" {
|
2023-04-13 22:37:13 +03:00
|
|
|
logger.Debug("wrong data ", inbound)
|
2023-06-08 13:20:35 +03:00
|
|
|
return false
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
settings := map[string][]model.Client{}
|
|
|
|
json.Unmarshal([]byte(inbound.Settings), &settings)
|
|
|
|
clients := settings["clients"]
|
2023-06-15 22:15:34 +03:00
|
|
|
shouldCleanLog := false
|
2023-02-28 22:54:29 +03:00
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
// create iplimit log file channel
|
|
|
|
logIpFile, err := os.OpenFile(xray.GetIPLimitLogPath(), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("failed to create or open ip limit log file: %s", err)
|
|
|
|
}
|
|
|
|
defer logIpFile.Close()
|
|
|
|
log.SetOutput(logIpFile)
|
|
|
|
log.SetFlags(log.LstdFlags)
|
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
for _, client := range clients {
|
|
|
|
if client.Email == clientEmail {
|
|
|
|
limitIp := client.LimitIP
|
2023-06-03 18:29:32 +03:00
|
|
|
|
2023-06-15 22:15:34 +03:00
|
|
|
if limitIp != 0 {
|
|
|
|
shouldCleanLog = true
|
|
|
|
|
|
|
|
if limitIp < len(ips) && inbound.Enable {
|
|
|
|
disAllowedIps = append(disAllowedIps, ips[limitIp:]...)
|
2023-06-16 00:38:35 +03:00
|
|
|
for i := limitIp; i < len(ips); i++ {
|
2023-06-24 23:36:18 +03:00
|
|
|
log.Printf("[LIMIT_IP] Email = %s || SRC = %s", clientEmail, ips[i])
|
2023-06-15 22:15:34 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-16 00:38:35 +03:00
|
|
|
}
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-03 18:29:32 +03:00
|
|
|
logger.Debug("disAllowedIps ", disAllowedIps)
|
|
|
|
sort.Strings(disAllowedIps)
|
2023-02-28 22:54:29 +03:00
|
|
|
|
|
|
|
db := database.GetDB()
|
|
|
|
err = db.Save(inboundClientIps).Error
|
|
|
|
if err != nil {
|
2023-06-15 22:15:34 +03:00
|
|
|
return shouldCleanLog
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
2023-06-15 22:15:34 +03:00
|
|
|
return shouldCleanLog
|
2023-02-28 22:54:29 +03:00
|
|
|
}
|
2023-06-08 13:20:35 +03:00
|
|
|
|
2023-07-01 15:26:43 +03:00
|
|
|
func (j *CheckClientIpJob) getInboundByEmail(clientEmail string) (*model.Inbound, error) {
|
2023-02-28 22:54:29 +03:00
|
|
|
db := database.GetDB()
|
|
|
|
var inbounds *model.Inbound
|
2023-07-01 15:26:43 +03:00
|
|
|
|
2023-04-13 22:37:13 +03:00
|
|
|
err := db.Model(model.Inbound{}).Where("settings LIKE ?", "%"+clientEmail+"%").Find(&inbounds).Error
|
2023-02-28 22:54:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-01 15:26:43 +03:00
|
|
|
|
2023-02-28 22:54:29 +03:00
|
|
|
return inbounds, nil
|
2023-06-16 00:38:35 +03:00
|
|
|
}
|