Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add record (aka request logs) list and edit pages #27

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions controllers/record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package controllers
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"github.com/casbin/caswaf/object"
)

func (c *ApiController) GetRecords() {
if c.RequireSignedIn() {
return
}

owner := c.Input().Get("owner")
if owner == "admin" {
owner = ""
}

sites, err := object.GetRecords(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

// object.GetMaskedSites(sites, util.GetHostname())
c.ResponseOk(sites)
}

func (c *ApiController) DeleteRecord() {
if c.RequireSignedIn() {
return
}

var record object.Record
err := json.Unmarshal(c.Ctx.Input.RequestBody, &record)
if err != nil {
c.ResponseError(err.Error())
return
}

c.Data["json"] = wrapActionResponse(object.DeleteRecord(&record))
c.ServeJSON()
}

func (c *ApiController) UpdateRecord() {
if c.RequireSignedIn() {
return
}

owner := c.Input().Get("owner")
id := c.Input().Get("id")

var record object.Record
err := json.Unmarshal(c.Ctx.Input.RequestBody, &record)
if err != nil {
c.ResponseError(err.Error())
return
}

c.Data["json"] = wrapActionResponse(object.UpdateRecord(owner, id, &record))
c.ServeJSON()
}

func (c *ApiController) GetRecord() {
if c.RequireSignedIn() {
return
}

owner := c.Input().Get("owner")
id := c.Input().Get("id")
record, err := object.GetRecord(owner, id)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(record)
}
5 changes: 5 additions & 0 deletions object/ormer.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,9 @@ func (a *Ormer) createTable() {
if err != nil {
panic(err)
}

err = a.Engine.Sync2(new(Record))
if err != nil {
panic(err)
}
}
86 changes: 86 additions & 0 deletions object/record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package object
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"github.com/xorm-io/core"
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
"strconv"
)

type Record struct {
Id int64 `xorm:"int notnull pk autoincr" json:"id"`
Owner string `xorm:"varchar(100) notnull" json:"owner"`
CreatedTime string `xorm:"varchar(100) notnull" json:"createdTime"`

Method string `xorm:"varchar(100)" json:"method"`
Host string `xorm:"varchar(100)" json:"host"`
RequestURI string `xorm:"varchar(100)" json:"requestURI"`
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
UserAgent string `xorm:"varchar(512)" json:"userAgent"`
}

func GetRecords(owner string) ([]*Record, error) {
records := []*Record{}
err := ormer.Engine.Asc("Id").Desc("created_time").Asc("Host").Asc("request_u_r_i").Find(&records, &Record{Owner: owner})
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

return records, nil
}

func AddRecord(record *Record) (bool, error) {
affected, err := ormer.Engine.Insert(record)
if err != nil {
return false, err
}

return affected != 0, nil
}

func DeleteRecord(record *Record) (bool, error) {
affected, err := ormer.Engine.ID(core.PK{record.Id}).Delete(&Record{})
if err != nil {
return false, err
}

return affected != 0, nil
}

func UpdateRecord(owner string, id string, record *Record) (bool, error) {
//id_num, err := strconv.ParseInt(id, 10, 64)
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
//if err != nil {
// fmt.Println("Failed to transform id(string) to num: ", err)
//}

affected, err := ormer.Engine.ID(core.PK{record.Id}).AllCols().Update(record)
if err != nil {
return false, err
}

return affected != 0, nil
}

func GetRecord(owner string, id string) (*Record, error) {
idNum, err := strconv.Atoi(id)
if err != nil {
fmt.Println("Failed to transform id(string) to num: ", err)
}
record, err := getRecord(owner, int64(idNum))
if err != nil {
return nil, err
}

return record, nil
}

func getRecord(owner string, id int64) (*Record, error) {
record := Record{Owner: owner, Id: id}
existed, err := ormer.Engine.Get(&record)
if err != nil {
return nil, err
}

if existed {
return &record, nil
}
return nil, nil
}
5 changes: 5 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ func initAPI() {
beego.Router("/api/delete-cert", &controllers.ApiController{}, "POST:DeleteCert")

beego.Router("/api/get-applications", &controllers.ApiController{}, "GET:GetApplications")

beego.Router("/api/get-records", &controllers.ApiController{}, "GET:GetRecords")
beego.Router("/api/get-record", &controllers.ApiController{}, "GET:GetRecord")
beego.Router("/api/delete-record", &controllers.ApiController{}, "POST:DeleteRecord")
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
beego.Router("/api/update-record", &controllers.ApiController{}, "POST:UpdateRecord")
}
13 changes: 13 additions & 0 deletions service/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/url"
"path/filepath"
"strings"
"time"

"github.com/beego/beego"
"github.com/casbin/caswaf/object"
Expand Down Expand Up @@ -84,6 +85,18 @@ func redirectToHost(w http.ResponseWriter, r *http.Request, host string) {
func handleRequest(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.UserAgent(), "Uptime-Kuma") {
fmt.Printf("handleRequest: %s\t%s\t%s\t%s\t%s\n", r.RemoteAddr, r.Method, r.Host, r.RequestURI, r.UserAgent())
nomeguy marked this conversation as resolved.
Show resolved Hide resolved
record := object.Record{
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
Owner: "admin",
CreatedTime: time.Now().Format("2006-01-02 15:04:05.000000"),
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
Method: r.Method,
Host: r.Host,
RequestURI: r.RequestURI,
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
UserAgent: r.UserAgent(),
}
_, err := object.AddRecord(&record)
if err != nil {
fmt.Println(err.Error())
}
}

site := getSiteByDomainWithWww(r.Host)
Expand Down
15 changes: 15 additions & 0 deletions web/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import SiteEditPage from "./SiteEditPage";
import CertListPage from "./CertListPage";
import CertEditPage from "./CertEditPage";
import SigninPage from "./SigninPage";
import RecordListPage from "./RecordListPage";

import i18next from "i18next";
import RecordEditPage from "./RecordEditPage";
HeartLinked marked this conversation as resolved.
Show resolved Hide resolved
// import SelectLanguageBox from "./SelectLanguageBox";

const {Header, Footer} = Layout;
Expand Down Expand Up @@ -71,6 +74,8 @@ class App extends Component {
this.setState({selectedMenuKey: "/sites"});
} else if (uri.includes("/certs")) {
this.setState({selectedMenuKey: "/certs"});
} else if (uri.includes("/records")) {
this.setState({selectedMenuKey: "/records"});
} else {
this.setState({selectedMenuKey: "null"});
}
Expand Down Expand Up @@ -253,6 +258,13 @@ class App extends Component {
</Menu.Item>
);

res.push(
<Menu.Item key="/records">
<Link to="/records">
{i18next.t("general:Records")}
</Link>
</Menu.Item>
);
return res;
}

Expand Down Expand Up @@ -310,6 +322,9 @@ class App extends Component {
<Route exact path="/sites/:owner/:siteName" render={(props) => this.renderSigninIfNotSignedIn(<SiteEditPage account={this.state.account} {...props} />)} />
<Route exact path="/certs" render={(props) => this.renderSigninIfNotSignedIn(<CertListPage account={this.state.account} {...props} />)} />
<Route exact path="/certs/:owner/:certName" render={(props) => this.renderSigninIfNotSignedIn(<CertEditPage account={this.state.account} {...props} />)} />

<Route exact path="/records" render={(props) => this.renderSigninIfNotSignedIn(<RecordListPage account={this.state.account} {...props} />)} />
<Route exact path="/records/:owner/:id" render={(props) => this.renderSigninIfNotSignedIn(<RecordEditPage account={this.state.account} {...props} />)} />
</Switch>
</div>
);
Expand Down
Loading
Loading