Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from proshir/main
Browse files Browse the repository at this point in the history
add vless limit ip
  • Loading branch information
hossinasaadi authored Nov 2, 2022
2 parents 60b649b + 74f7af6 commit b5a6882
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 70 deletions.
6 changes: 5 additions & 1 deletion web/assets/js/model/xray.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,16 +1215,20 @@ Inbound.VLESSSettings = class extends Inbound.Settings {
};
Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {

constructor(id=RandomUtil.randomUUID(), flow=FLOW_CONTROL.DIRECT) {
constructor(id=RandomUtil.randomUUID(), flow=FLOW_CONTROL.DIRECT, email='', limitIp=0) {
super();
this.id = id;
this.flow = flow;
this.email = email;
this.limitIp = limitIp;
}

static fromJson(json={}) {
return new Inbound.VLESSSettings.VLESS(
json.id,
json.flow,
json.email,
json.limitIp
);
}
};
Expand Down
34 changes: 34 additions & 0 deletions web/html/xui/form/protocol/vless.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
{{define "form/vless"}}
<a-form layout="inline">
<a-form layout="inline">
<a-form-item label="Email">
<a-input v-model.trim="inbound.settings.vlesses[0].email"></a-input>
</a-form-item>
<a-form-item>
<span slot="label">
IP Count Limit
<a-tooltip>
<template slot="title">
disable inbound if more than entered count (0 for disable limit ip)
</template>
<a-icon type="question-circle" theme="filled"></a-icon>
</a-tooltip>
</span>

<a-input type="number" v-model.number="inbound.settings.vlesses[0].limitIp"></a-input>
</a-form-item>
<a-form-item v-if="inbound.settings.vlesses[0].email && inbound.settings.vlesses[0].limitIp > 0 && isEdit">
<span slot="label">
client IP log
<a-tooltip>
<template slot="title">
IPs history Log (before enabling inbound after it has been disabled by IP limit, you should clear the log)
</template>
<a-icon type="question-circle" theme="filled"></a-icon>
</a-tooltip>
</span>

<a-textarea disabled :input="getDBClientIps(inbound.settings.vlesses[0].email)" v-model="clientIps" :auto-size="{ minRows: 3, maxRows: 3 }">
</a-textarea>

<a-button type="danger" @click="clearDBClientIps(inbound.settings.vlesses[0].email)" >clear log</a-button>
</a-form-item>
</a-form>
<a-form-item label="id">
<a-input v-model.trim="inbound.settings.vlesses[0].id"></a-input>
</a-form-item>
Expand Down
111 changes: 42 additions & 69 deletions web/job/check_clinet_ip_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewCheckClientIpJob() *CheckClientIpJob {
}

func (j *CheckClientIpJob) Run() {
logger.Debug("Check Client IP Job...")
processLogFile()
}

Expand Down Expand Up @@ -77,18 +78,21 @@ func processLogFile() {
}

}
for clientEmail, ips := range InboundClientIps {
inboundClientIps,err := GetInboundClientIps(clientEmail)
if(err != nil){
addInboundClientIps(clientEmail,ips)
err = ClearInboudClientIps()
if err != nil {
return
}

}else{
updateInboundClientIps(inboundClientIps,clientEmail,ips)
var inboundsClientIps []*model.InboundClientIps
for clientEmail, ips := range InboundClientIps {
inboundClientIps := GetInboundClientIps(clientEmail, ips)
if inboundClientIps != nil {
inboundsClientIps = append(inboundsClientIps, inboundClientIps)
}

}


err = AddInboundsClientIps(inboundsClientIps)
checkError(err)
}
func GetAccessLogPath() string {

Expand All @@ -97,9 +101,7 @@ func GetAccessLogPath() string {

jsonConfig := map[string]interface{}{}
err = json.Unmarshal([]byte(config), &jsonConfig)
if err != nil {
logger.Warning(err)
}
checkError(err)
if(jsonConfig["log"] != nil) {
jsonLog := jsonConfig["log"].(map[string]interface{})
if(jsonLog["access"] != nil) {
Expand All @@ -126,85 +128,55 @@ func contains(s []string, str string) bool {

return false
}
// https://codereview.stackexchange.com/a/192954
func Unique(slice []string) []string {
// create a map with all the values as key
uniqMap := make(map[string]struct{})
for _, v := range slice {
uniqMap[v] = struct{}{}
}

// turn the map keys into a slice
uniqSlice := make([]string, 0, len(uniqMap))
for v := range uniqMap {
uniqSlice = append(uniqSlice, v)
}
return uniqSlice
}

func GetInboundClientIps(clientEmail string) (*model.InboundClientIps, error) {
func ClearInboudClientIps() error {
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
}
func addInboundClientIps(clientEmail string,ips []string) error {
inboundClientIps := &model.InboundClientIps{}
jsonIps, err := json.Marshal(ips)
err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&model.InboundClientIps{}).Error
checkError(err)
return err
}

inboundClientIps.ClientEmail = clientEmail
inboundClientIps.Ips = string(jsonIps)


db := database.GetDB()
tx := db.Begin()

defer func() {
if err == nil {
tx.Commit()
} else {
tx.Rollback()
}
}()

err = tx.Save(inboundClientIps).Error
func GetInboundClientIps(clientEmail string, ips []string) *model.InboundClientIps {
jsonIps, err := json.Marshal(ips)
if err != nil {
return err
return nil
}
return nil
}
func updateInboundClientIps(inboundClientIps *model.InboundClientIps,clientEmail string,ips []string) error {

jsonIps, err := json.Marshal(ips)
checkError(err)

inboundClientIps := &model.InboundClientIps{}
inboundClientIps.ClientEmail = clientEmail
inboundClientIps.Ips = string(jsonIps)

// check inbound limitation
inbound, _ := GetInboundByEmail(clientEmail)

inbound, err := GetInboundByEmail(clientEmail)
if err != nil {
return nil
}
limitIpRegx, _ := regexp.Compile(`"limitIp": .+`)

limitIpMactch := limitIpRegx.FindString(inbound.Settings)
limitIpMactch = ss.Split(limitIpMactch, `"limitIp": `)[1]
limitIp, err := strconv.Atoi(limitIpMactch)


if err != nil {
return nil
}
if(limitIp < len(ips) && limitIp != 0 && inbound.Enable) {

DisableInbound(inbound.Id)
}

return inboundClientIps
}

func AddInboundsClientIps(inboundsClientIps []*model.InboundClientIps) error {
if inboundsClientIps == nil || len(inboundsClientIps) == 0 {
return nil
}
db := database.GetDB()
err = db.Save(inboundClientIps).Error
tx := db.Begin()

err := tx.Save(inboundsClientIps).Error
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}

Expand All @@ -217,7 +189,8 @@ func GetInboundByEmail(clientEmail string) (*model.Inbound, error) {
}
return inbounds, nil
}
func DisableInbound(id int) error{

func DisableInbound(id int) error {
db := database.GetDB()
result := db.Model(model.Inbound{}).
Where("id = ? and enable = ?", id, true).
Expand Down

0 comments on commit b5a6882

Please sign in to comment.