Skip to content

Commit

Permalink
geoip.dat geosite.dat 独立更新
Browse files Browse the repository at this point in the history
  • Loading branch information
qist committed Oct 19, 2022
1 parent fe02837 commit 2e1fb2b
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

更新日志:

2022.10.19 更新xray时不更新geoip.dat geosite.dat . geoip.dat geosite.dat 使用[Loyalsoldier](https://github.com/Loyalsoldier/geoip)提供版本单独更新

2022.10.17 更改trojan 可以关闭tls配置可以使用nginx 对外代理

2022.7.4 更新上游支持trojan的多类传输协议支持
Expand Down
2 changes: 1 addition & 1 deletion config/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1.2
0.1.1.3
66 changes: 66 additions & 0 deletions web/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ type ServerController struct {

lastVersions []string
lastGetVersionsTime time.Time

lastGeoipStatus *service.Status
lastGeoipGetStatusTime time.Time

lastGeoipVersions []string
lastGeoipGetVersionsTime time.Time

lastGeositeStatus *service.Status
lastGeositeGetStatusTime time.Time

lastGeositeVersions []string
lastGeositeGetVersionsTime time.Time
}

func NewServerController(g *gin.RouterGroup) *ServerController {
Expand All @@ -35,6 +47,10 @@ func (a *ServerController) initRouter(g *gin.RouterGroup) {
g.POST("/status", a.status)
g.POST("/getXrayVersion", a.getXrayVersion)
g.POST("/installXray/:version", a.installXray)
g.POST("/getGeoipVersion", a.getGeoipVersion)
g.POST("/installGeoip/:version", a.installGeoip)
g.POST("/getGeositeVersion", a.getGeositeVersion)
g.POST("/installGeosite/:version", a.installGeosite)
}

func (a *ServerController) refreshStatus() {
Expand Down Expand Up @@ -83,3 +99,53 @@ func (a *ServerController) installXray(c *gin.Context) {
err := a.serverService.UpdateXray(version)
jsonMsg(c, "安装 xray", err)
}

func (a *ServerController) getGeoipVersion(c *gin.Context) {
now := time.Now()
if now.Sub(a.lastGeoipGetVersionsTime) <= time.Minute {
jsonObj(c, a.lastGeoipVersions, nil)
return
}

versions, err := a.serverService.GetGeoipVersions()
if err != nil {
jsonMsg(c, "获取版本", err)
return
}

a.lastGeoipVersions = versions
a.lastGeoipGetVersionsTime = time.Now()

jsonObj(c, versions, nil)
}

func (a *ServerController) installGeoip(c *gin.Context) {
version := c.Param("version")
err := a.serverService.UpdateGeoip(version)
jsonMsg(c, "安装 geoip", err)
}

func (a *ServerController) getGeositeVersion(c *gin.Context) {
now := time.Now()
if now.Sub(a.lastGeositeGetVersionsTime) <= time.Minute {
jsonObj(c, a.lastGeositeVersions, nil)
return
}

versions, err := a.serverService.GetGeositeVersions()
if err != nil {
jsonMsg(c, "获取版本", err)
return
}

a.lastGeositeVersions = versions
a.lastGeositeGetVersionsTime = time.Now()

jsonObj(c, versions, nil)
}

func (a *ServerController) installGeosite(c *gin.Context) {
version := c.Param("version")
err := a.serverService.UpdateGeosite(version)
jsonMsg(c, "安装 geosite", err)
}
86 changes: 85 additions & 1 deletion web/html/xui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
</a-tooltip>
<a-tag color="green" @click="openSelectV2rayVersion">[[ status.xray.version ]]</a-tag>
<a-tag color="blue" @click="openSelectV2rayVersion">切换版本</a-tag>
<a-tag color="blue" @click="geoipUpdate">geoip更新</a-tag>
<a-tag color="blue" @click="geositeUpdate">geosite更新</a-tag>
</a-card>
</a-col>
<a-col :sm="24" :md="12">
Expand Down Expand Up @@ -176,6 +178,30 @@ <h2>请谨慎选择,旧版本可能配置不兼容</h2>
</a-tag>
</template>
</a-modal>
<a-modal id="version-modal" v-model="versionModal.geoip" title="geoip更新"
:closable="true" @ok="() => versionModal.geoip = false"
ok-text="确定" cancel-text="取消">
<h2>点击你想更新的版本</h2>
<!-- <h2>请谨慎选择,旧版本可能配置不兼容</h2> -->
<template v-for="version, index in versionModal.geoips">
<a-tag :color="index % 2 == 0 ? 'blue' : 'green'"
style="margin: 10px" @click="geoipUpdateVersion(version)">
[[ version ]]
</a-tag>
</template>
</a-modal>
<a-modal id="version-modal" v-model="versionModal.geosite" title="geosite更新"
:closable="true" @ok="() => versionModal.geosite = false"
ok-text="确定" cancel-text="取消">
<h2>点击你想更新的版本</h2>
<!-- <h2>请谨慎选择,旧版本可能配置不兼容</h2> -->
<template v-for="version, index in versionModal.geosites">
<a-tag :color="index % 2 == 0 ? 'blue' : 'green'"
style="margin: 10px" @click="geositeUpdateVersion(version)">
[[ version ]]
</a-tag>
</template>
</a-modal>
</a-layout>
{{template "js" .}}
<script>
Expand Down Expand Up @@ -259,11 +285,23 @@ <h2>请谨慎选择,旧版本可能配置不兼容</h2>

const versionModal = {
visible: false,
geoip: false,
geosite: false,
geosites: [],
versions: [],
geoips:[],
show(versions) {
this.visible = true;
this.versions = versions;
},
geoipshow(versions) {
this.geoip = true;
this.geoips = versions;
},
geositeshow(versions){
this.geosite = true;
this.geosites = versions;
},
hide() {
this.visible = false;
},
Expand Down Expand Up @@ -302,6 +340,24 @@ <h2>请谨慎选择,旧版本可能配置不兼容</h2>
}
versionModal.show(msg.obj);
},
async geoipUpdate(){
this.loading(true);
const msg = await HttpUtil.post('server/getGeoipVersion');
this.loading(false);
if (!msg.success) {
return;
}
versionModal.geoipshow(msg.obj);
},
async geositeUpdate(){
this.loading(true);
const msg = await HttpUtil.post('server/getGeositeVersion');
this.loading(false);
if (!msg.success) {
return;
}
versionModal.geositeshow(msg.obj);
},
switchV2rayVersion(version) {
this.$confirm({
title: '切换 xray 版本',
Expand All @@ -316,6 +372,34 @@ <h2>请谨慎选择,旧版本可能配置不兼容</h2>
},
});
},
geoipUpdateVersion(version){
this.$confirm({
title: '切换 geoip 版本',
content: '是否切换 geoip 版本至' + ` ${version}?`,
okText: '确定',
cancelText: '取消',
onOk: async () => {
versionModal.hide();
this.loading(true, '安装中,请不要刷新此页面');
await HttpUtil.post(`/server/installGeoip/${version}`);
this.loading(false);
},
});
},
geositeUpdateVersion(version){
this.$confirm({
title: '切换 geosite 版本',
content: '是否切换 geosite 版本至' + ` ${version}?`,
okText: '确定',
cancelText: '取消',
onOk: async () => {
versionModal.hide();
this.loading(true, '安装中,请不要刷新此页面');
await HttpUtil.post(`/server/installGeosite/${version}`);
this.loading(false);
},
});
},
},
async mounted() {
while (true) {
Expand All @@ -331,4 +415,4 @@ <h2>请谨慎选择,旧版本可能配置不兼容</h2>

</script>
</body>
</html>
</html>
145 changes: 143 additions & 2 deletions web/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,156 @@ func (s *ServerService) UpdateXray(version string) error {
if err != nil {
return err
}
err = copyZipFile("geosite.dat", xray.GetGeositePath())
// err = copyZipFile("geosite.dat", xray.GetGeositePath())
// if err != nil {
// return err
// }
// err = copyZipFile("geoip.dat", xray.GetGeoipPath())
// if err != nil {
// return err
// }

return nil

}


func (s *ServerService) GetGeoipVersions() ([]string, error) {
url := "https://api.github.com/repos/Loyalsoldier/geoip/releases"
resp, err := http.Get(url)
if err != nil {
return nil, err
}

defer resp.Body.Close()
buffer := bytes.NewBuffer(make([]byte, 8192))
buffer.Reset()
_, err = buffer.ReadFrom(resp.Body)
if err != nil {
return nil, err
}

releases := make([]Release, 0)
err = json.Unmarshal(buffer.Bytes(), &releases)
if err != nil {
return nil, err
}
versions := make([]string, 0, len(releases))
for _, release := range releases {
versions = append(versions, release.TagName)
}
return versions, nil
}

func (s *ServerService) downloadGeoip(version string) (string, error) {

fileName := fmt.Sprintf("geoip.dat")
url := fmt.Sprintf("https://github.com/Loyalsoldier/geoip/releases/download/%s/%s", version, fileName)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()

os.Remove(fileName)
file, err := os.Create(xray.GetGeoipPath())
if err != nil {
return "", err
}
defer file.Close()

_, err = io.Copy(file, resp.Body)
if err != nil {
return "", err
}

return fileName, nil
}

func (s *ServerService) UpdateGeoip(version string) error {
_, err := s.downloadGeoip(version)
if err != nil {
return err
}
err = copyZipFile("geoip.dat", xray.GetGeoipPath())

s.xrayService.StopXray()
defer func() {
err := s.xrayService.RestartXray(true)
if err != nil {
logger.Error("start xray failed:", err)
}
}()

return nil

}

func (s *ServerService) GetGeositeVersions() ([]string, error) {
url := "https://api.github.com/repos/Loyalsoldier/domain-list-custom/releases"
resp, err := http.Get(url)
if err != nil {
return nil, err
}

defer resp.Body.Close()
buffer := bytes.NewBuffer(make([]byte, 8192))
buffer.Reset()
_, err = buffer.ReadFrom(resp.Body)
if err != nil {
return nil, err
}

releases := make([]Release, 0)
err = json.Unmarshal(buffer.Bytes(), &releases)
if err != nil {
return nil, err
}
versions := make([]string, 0, len(releases))
for _, release := range releases {
versions = append(versions, release.TagName)
}
return versions, nil
}

func (s *ServerService) downloadGeosite(version string) (string, error) {

fileName := fmt.Sprintf("geosite.dat")
url := fmt.Sprintf("https://github.com/Loyalsoldier/domain-list-custom/releases/download/%s/%s", version, fileName)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()

os.Remove(fileName)
file, err := os.Create(xray.GetGeositePath())
if err != nil {
return "", err
}
defer file.Close()

_, err = io.Copy(file, resp.Body)
if err != nil {
return "", err
}

return fileName, nil
}

func (s *ServerService) UpdateGeosite(version string) error {
_, err := s.downloadGeosite(version)
if err != nil {
return err
}

s.xrayService.StopXray()
defer func() {
err := s.xrayService.RestartXray(true)
if err != nil {
logger.Error("start xray failed:", err)
}
}()

return nil

}

0 comments on commit 2e1fb2b

Please sign in to comment.