Skip to content

Commit

Permalink
format code and fix wrong spell
Browse files Browse the repository at this point in the history
  • Loading branch information
xuc2 committed Dec 3, 2021
1 parent 7fd539c commit 0bb3c92
Show file tree
Hide file tree
Showing 22 changed files with 90 additions and 100 deletions.
2 changes: 0 additions & 2 deletions model/system.go
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
package model


1 change: 0 additions & 1 deletion pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"golang.org/x/oauth2"
)


func GetGithubClient() *github.Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
Expand Down
4 changes: 2 additions & 2 deletions pkg/upnp/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func GetCtrlUrl(host,device string) string {
func GetCtrlUrl(host, device string) string {
request := ctrlUrlRequest(host, device)
response, _ := http.DefaultClient.Do(request)
resultBody, _ := ioutil.ReadAll(response.Body)
Expand Down Expand Up @@ -86,4 +86,4 @@ func resolve(resultStr string) string {
}
}
return controlURL
}
}
8 changes: 4 additions & 4 deletions pkg/upnp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ func send() (string, error) {
"ST: urn:schemas-upnp-org:service:WANIPConnection:1\r\n" +
"MAN: \"ssdp:discover\"\r\n" + "MX: 3\r\n\r\n"
var conn *net.UDPConn
remotAddr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
remoteAddr, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
if err != nil {
return "", errors.New("组播地址格式不正确")
}
locaAddr, err := net.ResolveUDPAddr("udp", ip_helper2.GetLoclIp()+":")
localAddr, err := net.ResolveUDPAddr("udp", ip_helper2.GetLoclIp()+":")

if err != nil {
return "", errors.New("本地ip地址格式不正确")
}
conn, err = net.ListenUDP("udp", locaAddr)
conn, err = net.ListenUDP("udp", localAddr)
defer conn.Close()
if err != nil {
return "", errors.New("监听udp出错")
}
_, err = conn.WriteToUDP([]byte(str), remotAddr)
_, err = conn.WriteToUDP([]byte(str), remoteAddr)
if err != nil {
return "", errors.New("发送msg到组播地址出错")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/upnp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import "testing"
func TestGateway(t *testing.T) {

Gateway()
}
}
53 changes: 24 additions & 29 deletions pkg/upnp/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@ package upnp

import (
"bytes"
"github.com/pkg/errors"
"net/http"
"strconv"
"strings"

loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
"github.com/pkg/errors"
)

//
////添加一个端口映射
func (n *Upnp)AddPortMapping(localPort, remotePort int, protocol string) (err error) {
defer func(err error) {
func (n *Upnp) AddPortMapping(localPort, remotePort int, protocol string) (err error) {
defer func() {
if errTemp := recover(); errTemp != nil {
//log.Println("upnp模块报错了", errTemp)
err = errTemp.(error)
loger2.NewOLoger().Error("upnp模块报错了", errTemp)
}
}(err)
if issuccess := addSend(localPort, remotePort, protocol,n.GatewayHost, n.CtrlUrl,n.LocalHost); issuccess {
}()

if isSuccess := addSend(localPort, remotePort, protocol, n.GatewayHost, n.CtrlUrl, n.LocalHost); isSuccess {
return nil
} else {
return errors.New("添加一个端口映射失败")
}
return
}

func addSend(localPort, remotePort int, protocol, host, ctrUrl,localHost string) bool {
request := addRequest(localPort, remotePort, protocol, host, ctrUrl,localHost)
func addSend(localPort, remotePort int, protocol, host, ctrUrl, localHost string) bool {
request := addRequest(localPort, remotePort, protocol, host, ctrUrl, localHost)
response, _ := http.DefaultClient.Do(request)
defer response.Body.Close()
//resultBody, _ := ioutil.ReadAll(response.Body)
//fmt.Println(string(resultBody))
if response.StatusCode == 200 {
return true
}

return false
return response.StatusCode == 200
}

type Node struct {
Expand All @@ -45,7 +42,7 @@ type Node struct {
Child []Node
}

func addRequest(localPort, remotePort int, protocol string, gatewayHost, ctlUrl,localHost string) *http.Request {
func addRequest(localPort, remotePort int, protocol string, gatewayHost, ctlUrl, localHost string) *http.Request {
//请求头
header := http.Header{}
header.Set("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
Expand Down Expand Up @@ -109,27 +106,25 @@ func (n *Node) BuildXML() string {
return buf.String()
}

func (n *Upnp)DelPortMapping(remotePort int, protocol string) bool {
issuccess := delSendSend(remotePort, protocol,n.GatewayHost,n.CtrlUrl)
if issuccess {
func (n *Upnp) DelPortMapping(remotePort int, protocol string) bool {
isSuccess := delSendSend(remotePort, protocol, n.GatewayHost, n.CtrlUrl)
if isSuccess {
//this.MappingPort.delMapping(remotePort, protocol)
//fmt.Println("删除了一个端口映射: remote:", remotePort)
}
return issuccess
return isSuccess
}

func delSendSend(remotePort int, protocol,host,ctlUrl string) bool {
delrequest := delbuildRequest(remotePort, protocol,host,ctlUrl)
func delSendSend(remotePort int, protocol, host, ctlUrl string) bool {
delrequest := delbuildRequest(remotePort, protocol, host, ctlUrl)
response, _ := http.DefaultClient.Do(delrequest)
//resultBody, _ := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if response.StatusCode == 200 {
// log.Println(string(resultBody))
return true
}
return false

return response.StatusCode == 200
}
func delbuildRequest(remotePort int, protocol,host,ctlUrl string) *http.Request {

func delbuildRequest(remotePort int, protocol, host, ctlUrl string) *http.Request {
//请求头
header := http.Header{}
header.Set("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
Expand Down Expand Up @@ -160,4 +155,4 @@ func delbuildRequest(remotePort int, protocol,host,ctlUrl string) *http.Request
request.Header = header
request.Header.Set("Content-Length", strconv.Itoa(len([]byte(bodyStr))))
return request
}
}
7 changes: 3 additions & 4 deletions pkg/upnp/upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

type Upnp struct {
LocalHost string `json:"local_host"`
GatewayName string `json:"gateway_name"` //网关名称
GatewayHost string `json:"gateway_host"` //网关ip和端口
GatewayName string `json:"gateway_name"` //网关名称
GatewayHost string `json:"gateway_host"` //网关ip和端口
DeviceDescUrl string `json:"device_desc_url"` //设备描述url
CtrlUrl string `json:"ctrl_url"` //控制请求url
CtrlUrl string `json:"ctrl_url"` //控制请求url
}

func Testaaa() {
Expand All @@ -23,4 +23,3 @@ func Testaaa() {
fmt.Println("gateway ip address: ", upnpMan.Gateway.Host)
}
}

2 changes: 1 addition & 1 deletion pkg/upnp/upnp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import (

func TestTestaaa(t *testing.T) {
(Testaaa())
}
}
2 changes: 1 addition & 1 deletion pkg/utils/loger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"path/filepath"
"runtime"

"github.com/IceWhaleTech/CasaOS/pkg/config"
file2 "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
)
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/random/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func TestRandomString(t *testing.T) {
fmt.Println(RandomString(6,true))
}
fmt.Println(RandomString(6, true))
}
2 changes: 1 addition & 1 deletion route/v1/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func DDNSAddConfig(c *gin.Context) {
})
return
}
var m model2.DDNSUpdataDBModel
var m model2.DDNSUpdateDBModel
c.Bind(&m)
if err := service.MyService.DDNS().SaveConfig(m); err != nil {
c.JSON(http.StatusOK,
Expand Down
6 changes: 3 additions & 3 deletions route/v1/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ func InstallApp(c *gin.Context) {
rely.ContainerId = mysqlContainerId
rely.CustomId = mid
rely.ContainerCustomId = id
var msqlConfig model2.MysqlConfigs
var mysqlConfig model2.MysqlConfigs

//结构体转换
copier.Copy(&msqlConfig, &mc)
rely.Config = msqlConfig
copier.Copy(&mysqlConfig, &mc)
rely.Config = mysqlConfig
service.MyService.Rely().Create(rely)

relyMap["mysql"] = mid
Expand Down
2 changes: 1 addition & 1 deletion route/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func GetSystemConfigDebug(c *gin.Context) {

array := service.MyService.System().GetSystemConfigDebug()
disk := service.MyService.ZiMa().GetDiskInfo()
array = append(array, fmt.Sprintf("disk,totle:%v,used:%v,UsedPercent:%v", disk.Total>>20, disk.Used>>20, disk.UsedPercent))
array = append(array, fmt.Sprintf("disk,total:%v,used:%v,UsedPercent:%v", disk.Total>>20, disk.Used>>20, disk.UsedPercent))

c.JSON(http.StatusOK, model.Result{Success: oasis_err.SUCCESS, Message: oasis_err.GetMsg(oasis_err.SUCCESS), Data: array})
}
Expand Down
10 changes: 6 additions & 4 deletions service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func (a *appStruct) GetMyList(index, size int, position bool) *[]model2.MyAppLis
for _, container := range containers {

if lMap[container.ID] != nil && container.Labels["origin"] != "system" {
var m model2.AppListDBModel
m = lMap[container.ID].(model2.AppListDBModel)
m := lMap[container.ID].(model2.AppListDBModel)
if len(m.Label) == 0 {
m.Label = m.Title
}
Expand Down Expand Up @@ -129,8 +128,7 @@ func (a *appStruct) GetSystemAppList() *[]model2.MyAppList {
for _, container := range containers {

if lMap[container.ID] != nil {
var m model2.AppListDBModel
m = lMap[container.ID].(model2.AppListDBModel)
m := lMap[container.ID].(model2.AppListDBModel)
if len(m.Label) == 0 {
m.Label = m.Title
}
Expand Down Expand Up @@ -194,6 +192,10 @@ func (a *appStruct) GetSimpleContainerInfo(name string) (types.Container, error)
filters := filters.NewArgs()
filters.Add("name", name)
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true, Filters: filters})
if err != nil {
return types.Container{}, err
}

if len(containers) > 0 {
return containers[0], nil
}
Expand Down
19 changes: 9 additions & 10 deletions service/ddns.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package service

import (
"os/exec"

ip_helper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/ip_helper"
loger2 "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
"github.com/IceWhaleTech/CasaOS/service/ddns"
"github.com/IceWhaleTech/CasaOS/service/model"
"gorm.io/gorm"
"os/exec"
)

type ddnsStruct struct {
Expand All @@ -20,17 +21,15 @@ type DDNSService interface {
GetConfigList() *[]model.DDNSList
DeleteConfig(id uint) bool
GetType(name string) (uint, string)
SaveConfig(model model.DDNSUpdataDBModel) error
SaveConfig(model model.DDNSUpdateDBModel) error
}

//判断当前添加的是否存在
func (d *ddnsStruct) IsExis(t int, domain string, host string) bool {
var count int64
d.db.Table(model.DDNSLISTTABLENAME).Where("type=? AND domain=? AND host=?", t, domain, host).Count(&count)
if count > 0 {
return true
}
return false

return count > 0
}

//前台获取已配置的ddns列表
Expand All @@ -41,7 +40,7 @@ func (d *ddnsStruct) GetConfigList() *[]model.DDNSList {
}

func (d *ddnsStruct) DeleteConfig(id uint) bool {
d.db.Delete(&model.DDNSUpdataDBModel{Id: id})
d.db.Delete(&model.DDNSUpdateDBModel{Id: id})
return true
}

Expand All @@ -66,12 +65,12 @@ func (d *ddnsStruct) GetType(name string) (uint, string) {
}

//保存配置到数据库
func (d *ddnsStruct) GetDockerRootDir(model model.DDNSUpdataDBModel) error {
func (d *ddnsStruct) GetDockerRootDir(model model.DDNSUpdateDBModel) error {
return d.db.Create(&model).Error
}

//保存配置到数据库
func (d *ddnsStruct) SaveConfig(model model.DDNSUpdataDBModel) error {
func (d *ddnsStruct) SaveConfig(model model.DDNSUpdateDBModel) error {
return d.db.Create(&model).Error
}

Expand All @@ -87,7 +86,7 @@ func chackPing(b chan bool, url string) {
}

//更新列表
func UpdataDDNSList(db *gorm.DB) {
func UpdateDDNSList(db *gorm.DB) {
var s []model.DDNSCoreList
db.Table(model.DDNSLISTTABLENAME).Select("o_ddns_type.name as name,o_ddns_type.api_host as api_host,o_ddns.id,`host`,domain,user_name,`password`,`key`,secret,type").Joins("left join o_ddns_type on o_ddns.type=o_ddns_type.id").Scan(&s)
for _, item := range s {
Expand Down
1 change: 0 additions & 1 deletion service/ddns/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ func SetOauth(request *http.Request, value string) {
func SetXFilter(request *http.Request, value string) {
request.Header.Set("X-Filter", value)
}

Loading

0 comments on commit 0bb3c92

Please sign in to comment.