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

Commit b5a6882

Browse files
authored
Merge pull request #1 from proshir/main
add vless limit ip
2 parents 60b649b + 74f7af6 commit b5a6882

File tree

3 files changed

+81
-70
lines changed

3 files changed

+81
-70
lines changed

web/assets/js/model/xray.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,16 +1215,20 @@ Inbound.VLESSSettings = class extends Inbound.Settings {
12151215
};
12161216
Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
12171217

1218-
constructor(id=RandomUtil.randomUUID(), flow=FLOW_CONTROL.DIRECT) {
1218+
constructor(id=RandomUtil.randomUUID(), flow=FLOW_CONTROL.DIRECT, email='', limitIp=0) {
12191219
super();
12201220
this.id = id;
12211221
this.flow = flow;
1222+
this.email = email;
1223+
this.limitIp = limitIp;
12221224
}
12231225

12241226
static fromJson(json={}) {
12251227
return new Inbound.VLESSSettings.VLESS(
12261228
json.id,
12271229
json.flow,
1230+
json.email,
1231+
json.limitIp
12281232
);
12291233
}
12301234
};

web/html/xui/form/protocol/vless.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
{{define "form/vless"}}
22
<a-form layout="inline">
3+
<a-form layout="inline">
4+
<a-form-item label="Email">
5+
<a-input v-model.trim="inbound.settings.vlesses[0].email"></a-input>
6+
</a-form-item>
7+
<a-form-item>
8+
<span slot="label">
9+
IP Count Limit
10+
<a-tooltip>
11+
<template slot="title">
12+
disable inbound if more than entered count (0 for disable limit ip)
13+
</template>
14+
<a-icon type="question-circle" theme="filled"></a-icon>
15+
</a-tooltip>
16+
</span>
17+
18+
<a-input type="number" v-model.number="inbound.settings.vlesses[0].limitIp"></a-input>
19+
</a-form-item>
20+
<a-form-item v-if="inbound.settings.vlesses[0].email && inbound.settings.vlesses[0].limitIp > 0 && isEdit">
21+
<span slot="label">
22+
client IP log
23+
<a-tooltip>
24+
<template slot="title">
25+
IPs history Log (before enabling inbound after it has been disabled by IP limit, you should clear the log)
26+
</template>
27+
<a-icon type="question-circle" theme="filled"></a-icon>
28+
</a-tooltip>
29+
</span>
30+
31+
<a-textarea disabled :input="getDBClientIps(inbound.settings.vlesses[0].email)" v-model="clientIps" :auto-size="{ minRows: 3, maxRows: 3 }">
32+
</a-textarea>
33+
34+
<a-button type="danger" @click="clearDBClientIps(inbound.settings.vlesses[0].email)" >clear log</a-button>
35+
</a-form-item>
36+
</a-form>
337
<a-form-item label="id">
438
<a-input v-model.trim="inbound.settings.vlesses[0].id"></a-input>
539
</a-form-item>

web/job/check_clinet_ip_job.go

Lines changed: 42 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func NewCheckClientIpJob() *CheckClientIpJob {
2626
}
2727

2828
func (j *CheckClientIpJob) Run() {
29+
logger.Debug("Check Client IP Job...")
2930
processLogFile()
3031
}
3132

@@ -77,18 +78,21 @@ func processLogFile() {
7778
}
7879

7980
}
80-
for clientEmail, ips := range InboundClientIps {
81-
inboundClientIps,err := GetInboundClientIps(clientEmail)
82-
if(err != nil){
83-
addInboundClientIps(clientEmail,ips)
81+
err = ClearInboudClientIps()
82+
if err != nil {
83+
return
84+
}
8485

85-
}else{
86-
updateInboundClientIps(inboundClientIps,clientEmail,ips)
86+
var inboundsClientIps []*model.InboundClientIps
87+
for clientEmail, ips := range InboundClientIps {
88+
inboundClientIps := GetInboundClientIps(clientEmail, ips)
89+
if inboundClientIps != nil {
90+
inboundsClientIps = append(inboundsClientIps, inboundClientIps)
8791
}
88-
8992
}
9093

91-
94+
err = AddInboundsClientIps(inboundsClientIps)
95+
checkError(err)
9296
}
9397
func GetAccessLogPath() string {
9498

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

98102
jsonConfig := map[string]interface{}{}
99103
err = json.Unmarshal([]byte(config), &jsonConfig)
100-
if err != nil {
101-
logger.Warning(err)
102-
}
104+
checkError(err)
103105
if(jsonConfig["log"] != nil) {
104106
jsonLog := jsonConfig["log"].(map[string]interface{})
105107
if(jsonLog["access"] != nil) {
@@ -126,85 +128,55 @@ func contains(s []string, str string) bool {
126128

127129
return false
128130
}
129-
// https://codereview.stackexchange.com/a/192954
130-
func Unique(slice []string) []string {
131-
// create a map with all the values as key
132-
uniqMap := make(map[string]struct{})
133-
for _, v := range slice {
134-
uniqMap[v] = struct{}{}
135-
}
136-
137-
// turn the map keys into a slice
138-
uniqSlice := make([]string, 0, len(uniqMap))
139-
for v := range uniqMap {
140-
uniqSlice = append(uniqSlice, v)
141-
}
142-
return uniqSlice
143-
}
144131

145-
func GetInboundClientIps(clientEmail string) (*model.InboundClientIps, error) {
132+
func ClearInboudClientIps() error {
146133
db := database.GetDB()
147-
InboundClientIps := &model.InboundClientIps{}
148-
err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
149-
if err != nil {
150-
return nil, err
151-
}
152-
return InboundClientIps, nil
153-
}
154-
func addInboundClientIps(clientEmail string,ips []string) error {
155-
inboundClientIps := &model.InboundClientIps{}
156-
jsonIps, err := json.Marshal(ips)
134+
err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&model.InboundClientIps{}).Error
157135
checkError(err)
136+
return err
137+
}
158138

159-
inboundClientIps.ClientEmail = clientEmail
160-
inboundClientIps.Ips = string(jsonIps)
161-
162-
163-
db := database.GetDB()
164-
tx := db.Begin()
165-
166-
defer func() {
167-
if err == nil {
168-
tx.Commit()
169-
} else {
170-
tx.Rollback()
171-
}
172-
}()
173-
174-
err = tx.Save(inboundClientIps).Error
139+
func GetInboundClientIps(clientEmail string, ips []string) *model.InboundClientIps {
140+
jsonIps, err := json.Marshal(ips)
175141
if err != nil {
176-
return err
142+
return nil
177143
}
178-
return nil
179-
}
180-
func updateInboundClientIps(inboundClientIps *model.InboundClientIps,clientEmail string,ips []string) error {
181-
182-
jsonIps, err := json.Marshal(ips)
183-
checkError(err)
184144

145+
inboundClientIps := &model.InboundClientIps{}
185146
inboundClientIps.ClientEmail = clientEmail
186147
inboundClientIps.Ips = string(jsonIps)
187-
188-
// check inbound limitation
189-
inbound, _ := GetInboundByEmail(clientEmail)
190148

149+
inbound, err := GetInboundByEmail(clientEmail)
150+
if err != nil {
151+
return nil
152+
}
191153
limitIpRegx, _ := regexp.Compile(`"limitIp": .+`)
192-
193154
limitIpMactch := limitIpRegx.FindString(inbound.Settings)
194155
limitIpMactch = ss.Split(limitIpMactch, `"limitIp": `)[1]
195156
limitIp, err := strconv.Atoi(limitIpMactch)
196-
197-
157+
if err != nil {
158+
return nil
159+
}
198160
if(limitIp < len(ips) && limitIp != 0 && inbound.Enable) {
199-
200161
DisableInbound(inbound.Id)
201162
}
202163

164+
return inboundClientIps
165+
}
166+
167+
func AddInboundsClientIps(inboundsClientIps []*model.InboundClientIps) error {
168+
if inboundsClientIps == nil || len(inboundsClientIps) == 0 {
169+
return nil
170+
}
203171
db := database.GetDB()
204-
err = db.Save(inboundClientIps).Error
172+
tx := db.Begin()
173+
174+
err := tx.Save(inboundsClientIps).Error
205175
if err != nil {
176+
tx.Rollback()
206177
return err
207178
}
179+
tx.Commit()
208180
return nil
209181
}
210182

@@ -217,7 +189,8 @@ func GetInboundByEmail(clientEmail string) (*model.Inbound, error) {
217189
}
218190
return inbounds, nil
219191
}
220-
func DisableInbound(id int) error{
192+
193+
func DisableInbound(id int) error {
221194
db := database.GetDB()
222195
result := db.Model(model.Inbound{}).
223196
Where("id = ? and enable = ?", id, true).

0 commit comments

Comments
 (0)