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

Commit

Permalink
Random Email (#97)
Browse files Browse the repository at this point in the history
* add random email

* Update inbound_modal.html

* Update inbound_modal.html

* [bug] fix client nil for other types

Co-authored-by: Hossin Asaadi <[email protected]>
  • Loading branch information
proshir and hossinasaadi committed Nov 21, 2022
1 parent 41cba88 commit f109991
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
2 changes: 1 addition & 1 deletion web/controller/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (a *BaseController) checkLogin(c *gin.Context) {
}


func I18n(c *gin.Context , name string, data ...string) string{
func I18n(c *gin.Context , name string) string{
anyfunc, _ := c.Get("I18n")
i18n, _ := anyfunc.(func(key string, params ...string) (string, error))

Expand Down
14 changes: 13 additions & 1 deletion web/html/xui/form/protocol/vless.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
<a-tag v-if="isExpiry(index) || ((getUpStats(vless.email) + getDownStats(vless.email)) > vless.totalGB && vless.totalGB != 0)" color="red" style="margin-bottom: 10px;display: block;text-align: center;">Account is (Expired|Traffic Ended) And Disabled</a-tag>

<a-form layout="inline">
<a-form-item label="Email">
<a-form-item>
<span slot="label">
Email
<a-tooltip>
<template slot="title">
The email must be completely unique
</template>
<!--Renew Svg Icon-->
<svg
@click="getNewEmail(vless)"
xmlns="http:https://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="anticon anticon-question-circle" viewBox="0 0 16 16"> <path d="M11.534 7h3.932a.25.25 0 0 1 .192.41l-1.966 2.36a.25.25 0 0 1-.384 0l-1.966-2.36a.25.25 0 0 1 .192-.41zm-11 2h3.932a.25.25 0 0 0 .192-.41L2.692 6.23a.25.25 0 0 0-.384 0L.342 8.59A.25.25 0 0 0 .534 9z"/> <path fill-rule="evenodd" d="M8 3c-1.552 0-2.94.707-3.857 1.818a.5.5 0 1 1-.771-.636A6.002 6.002 0 0 1 13.917 7H12.9A5.002 5.002 0 0 0 8 3zM3.1 9a5.002 5.002 0 0 0 8.757 2.182.5.5 0 1 1 .771.636A6.002 6.002 0 0 1 2.083 9H3.1z"/> </svg>
</a-tooltip>
</span>
<a-input v-model.trim="vless.email"></a-input>
</a-form-item>
<a-form-item>
Expand Down
14 changes: 13 additions & 1 deletion web/html/xui/form/protocol/vmess.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
<a-tag v-if="isExpiry(index) || ((getUpStats(vmess.email) + getDownStats(vmess.email)) > vmess.totalGB && vmess.totalGB != 0)" color="red" style="margin-bottom: 10px;display: block;text-align: center;">Account is (Expired|Traffic Ended) And Disabled</a-tag>

<a-form layout="inline">
<a-form-item label="Email">
<a-form-item>
<span slot="label">
Email
<a-tooltip>
<template slot="title">
The email must be completely unique
</template>
<!--Renew Svg Icon-->
<svg
@click="getNewEmail(vmess)"
xmlns="http:https://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="anticon anticon-question-circle" viewBox="0 0 16 16"> <path d="M11.534 7h3.932a.25.25 0 0 1 .192.41l-1.966 2.36a.25.25 0 0 1-.384 0l-1.966-2.36a.25.25 0 0 1 .192-.41zm-11 2h3.932a.25.25 0 0 0 .192-.41L2.692 6.23a.25.25 0 0 0-.384 0L.342 8.59A.25.25 0 0 0 .534 9z"/> <path fill-rule="evenodd" d="M8 3c-1.552 0-2.94.707-3.857 1.818a.5.5 0 1 1-.771-.636A6.002 6.002 0 0 1 13.917 7H12.9A5.002 5.002 0 0 0 8 3zM3.1 9a5.002 5.002 0 0 0 8.757 2.182.5.5 0 1 1 .771.636A6.002 6.002 0 0 1 2.083 9H3.1z"/> </svg>
</a-tooltip>
</span>
<a-input v-model.trim="vmess.email"></a-input>
</a-form-item>
<a-form-item>
Expand Down
12 changes: 10 additions & 2 deletions web/html/xui/inbound_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,17 @@
return email + (this.isClientEnable(email) == true ? ' Active' : ' Deactive')
},


getNewEmail(client) {
var chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
var string = '';
var len = 6 + Math.floor(Math.random() * 5)
for(var ii=0; ii<len; ii++){
string += chars[Math.floor(Math.random() * chars.length)];
}
client.email = string + "@gmail.com"
}
},
});

</script>
{{end}}
{{end}}
75 changes: 75 additions & 0 deletions web/service/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,64 @@ func (s *InboundService) checkPortExist(port int, ignoreId int) (bool, error) {
return count > 0, nil
}

func (s *InboundService) getClients(inbound *model.Inbound) ([]model.Client, error) {
settings := map[string][]model.Client{}
json.Unmarshal([]byte(inbound.Settings), &settings)
if settings == nil {
return nil, fmt.Errorf("Setting is null")
}

clients := settings["clients"]
if clients == nil {
return nil, nil
}
return clients, nil
}

func (s *InboundService) checkEmailsExist(emails map[string] bool, ignoreId int) (string, error) {
db := database.GetDB()
var inbounds []*model.Inbound
db = db.Model(model.Inbound{}).Where("Protocol in ?", []model.Protocol{model.VMess, model.VLESS})
if (ignoreId > 0) {
db = db.Where("id != ?", ignoreId)
}
db = db.Find(&inbounds)
if db.Error != nil {
return "", db.Error
}

for _, inbound := range inbounds {
clients, err := s.getClients(inbound)
if err != nil {
return "", err
}

for _, client := range clients {
if emails[client.Email] {
return client.Email, nil
}
}
}
return "", nil
}

func (s *InboundService) checkEmailExistForInbound(inbound *model.Inbound) (string, error) {
clients, err := s.getClients(inbound)
if err != nil {
return "", err
}
emails := make(map[string] bool)
for _, client := range clients {
if (client.Email != "") {
if emails[client.Email] {
return client.Email, nil
}
emails[client.Email] = true;
}
}
return s.checkEmailsExist(emails, inbound.Id)
}

func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound,error) {
exist, err := s.checkPortExist(inbound.Port, 0)
if err != nil {
Expand All @@ -58,6 +116,15 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound,erro
if exist {
return inbound, common.NewError("端口已存在:", inbound.Port)
}

existEmail, err := s.checkEmailExistForInbound(inbound)
if err != nil {
return inbound, err
}
if existEmail != "" {
return inbound, common.NewError("Duplicate email:", existEmail)
}

db := database.GetDB()

err = db.Save(inbound).Error
Expand Down Expand Up @@ -122,6 +189,14 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound,
if exist {
return inbound, common.NewError("端口已存在:", inbound.Port)
}

existEmail, err := s.checkEmailExistForInbound(inbound)
if err != nil {
return inbound, err
}
if existEmail != "" {
return inbound, common.NewError("Duplicate email:", existEmail)
}

oldInbound, err := s.GetInbound(inbound.Id)
if err != nil {
Expand Down

0 comments on commit f109991

Please sign in to comment.