Ma Suhyeon

Change framework to echo

# Created by https://www.toptal.com/developers/gitignore/api/go,vscode
# Edit at https://www.toptal.com/developers/gitignore?templates=go,vscode
### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
### Go Patch ###
/vendor/
/Godeps/
### vscode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# End of https://www.toptal.com/developers/gitignore/api/go,vscode
__debug_bin
config.json
data
mf-server
\ No newline at end of file
package main
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Prop int
const (
PropUserNo Prop = iota
)
type App struct {
Config Config
db *sqlx.DB
echo *echo.Echo
}
func NewApp(config Config) *App {
app := new(App)
app.Config = config
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", config.Database.User, config.Database.Password, config.Database.Host, config.Database.Name)
app.db = sqlx.MustOpen("mysql", dsn)
auth := middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte(config.TokenSecret),
Claims: &AuthClaims{},
})
app.echo = echo.New()
app.echo.POST("/users", app.PostUsers)
app.echo.POST("/users/tokens", app.PostTokens)
app.echo.POST("/extractions", app.PostExtractions, auth)
extraction := app.echo.Group("/extractions/:no")
extraction.GET("/calls", app.GetCalls)
extraction.GET("/messages", app.GetMessages)
extraction.GET("/calls/analyses", app.GetCallsAnalyses)
extraction.GET("/apps/analyses", app.GetAppsAnalyses)
extraction.GET("/messages/analyses", app.GetMessagesAnalyses)
extraction.GET("/processes", app.GetProcesses)
extraction.GET("/alarms", app.GetAlarms)
return app
}
func (app *App) Serve() {
app.echo.Logger.Fatal(app.echo.Start(fmt.Sprintf(":%d", app.Config.Port)))
}
package main
import (
"encoding/json"
"io/ioutil"
)
type Config struct {
Port int `json:"port"`
Database struct {
Host string `json:"host"`
Name string `json:"name"`
User string `json:"user"`
Password string `json:"password"`
} `json:"database"`
TokenSecret string `json:"token_secret"`
}
func LoadConfig(path string) (Config, error) {
config := Config{}
data, err := ioutil.ReadFile(path)
if err == nil {
err = json.Unmarshal(data, &config)
}
return config, err
}
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
_ "github.com/mattn/go-sqlite3"
)
type Call struct {
ID int `json:"id" db:"id"`
Type int `json:"type" db:"type"`
Name *string `json:"name" db:"name"`
Number int `json:"number" db:"number"`
Duration int `json:"duration" db:"duration"`
Date time.Time `json:"date" db:"date"`
}
func (app *App) GetCalls(c echo.Context) error {
calls := []Call{}
query := `SELECT * FROM calls WHERE extraction_no=?`
app.db.Unsafe().Select(&calls, query, c.Param("no"))
return c.JSON(http.StatusOK, calls)
}
type CallStats struct {
Number string `json:"number" db:"number"`
Name *string `json:"name" db:"name"`
Incoming int `json:"incoming" db:"incoming"`
Outgoing int `json:"outgoing" db:"outgoing"`
Duration int `json:"duration" db:"duration"`
}
func (app *App) GetCallsAnalyses(c echo.Context) error {
calls := []CallStats{}
query := `SELECT number, name,
(SELECT COUNT(1) FROM calls s WHERE s.extraction_no=c.extraction_no AND s.number=c.number AND s.type=1) incoming,
(SELECT COUNT(1) FROM calls s WHERE s.extraction_no=c.extraction_no AND s.number=c.number AND s.type=2) outgoing,
SUM(duration) duration
FROM calls c WHERE extraction_no=? GROUP BY number ORDER BY duration DESC`
app.db.Select(&calls, query, c.Param("no"))
return c.JSON(http.StatusOK, calls)
}
type AppInfo struct {
PackageName string `json:"package_name" db:"package_name"`
Name string `json:"name" db:"name"`
Version string `json:"version" db:"version"`
WifiUsage int `json:"wifi_usage" db:"wifi_usage"`
CellularUsage int `json:"cellular_usage" db:"cellular_usage"`
LastUsed time.Time `json:"last_used" db:"last_used"`
ForegroundTime int64 `json:"foreground_time" db:"foreground_time"`
}
func (app *App) GetAppsAnalyses(c echo.Context) error {
apps := []AppInfo{}
query := `SELECT * FROM apps WHERE extraction_no=? ORDER BY foreground_time DESC LIMIT 0, 100`
app.db.Unsafe().Select(&apps, query, c.Param("no"))
return c.JSON(http.StatusOK, apps)
}
type Message struct {
ID int `json:"id" db:"id"`
Type int `json:"type" db:"type"`
Address string `json:"address" db:"address"`
Body string `json:"body" db:"body"`
Date time.Time `json:"date" db:"date"`
}
func (app *App) GetMessages(c echo.Context) error {
messages := []Message{}
query := `SELECT * FROM messages WHERE extraction_no=?`
app.db.Unsafe().Select(&messages, query, c.Param("no"))
return c.JSON(http.StatusOK, messages)
}
type MessageStats struct {
Address string `json:"address" db:"address"`
Receive int `json:"receive" db:"receive"`
Send int `json:"send" db:"send"`
}
func (app *App) GetMessagesAnalyses(c echo.Context) error {
messages := []MessageStats{}
query := `SELECT address,
(SELECT COUNT(1) FROM messages m WHERE m.extraction_no=s.extraction_no AND m.address=s.address AND m.type=1) receive,
(SELECT COUNT(1) FROM messages m WHERE m.extraction_no=s.extraction_no AND m.address=s.address AND m.type=2) send
FROM messages s WHERE extraction_no=? GROUP BY address ORDER BY receive + send DESC`
app.db.Select(&messages, query, c.Param("no"))
return c.JSON(http.StatusOK, messages)
}
type Process struct {
UID string `json:"uid" db:"UID"`
PID int `json:"pid" db:"PID"`
PPID int `json:"ppid" db:"PPID"`
STIME string `json:"stime" db:"STIME"`
TIME string `json:"time" db:"TIME"`
CMD string `json:"cmd" db:"CMD"`
}
func (app *App) GetProcesses(c echo.Context) error {
processes := []Process{}
db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", c.Param("file")))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file")
}
defer db.Close()
query := `SELECT UID, CAST(PID AS INTEGER) PID, CAST(PPID AS INTEGER) PPID, STIME, TIME, CMD FROM process WHERE UID LIKE 'u%' ORDER BY TIME DESC`
db.Select(&processes, query)
return c.JSON(http.StatusOK, processes)
}
type Alarm struct {
ID string `json:"id"`
When time.Time `json:"when"`
History []AlarmHistory `json:"history"`
}
type AlarmHistory struct {
Type string `json:"type"`
When time.Time `json:"when"`
}
func (app *App) GetAlarms(c echo.Context) error {
db, err := sqlx.Connect("sqlite3", fmt.Sprintf("data/1/%s", c.Param("file")))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file")
}
defer db.Close()
alarms := map[string]Alarm{}
rows, _ := db.Queryx("SELECT * FROM alarm ORDER BY TIME")
for rows.Next() {
var tm string
var typ string
var detail string
rows.Scan(&tm, &typ, &detail)
detail = detail[strings.Index(detail, "{")+1 : strings.Index(detail, "}")]
s := strings.Split(detail, " ")
timestamp, _ := strconv.ParseInt(s[4], 10, 64)
timestamp /= 1000
if _, ok := alarms[s[0]]; !ok {
alarms[s[0]] = Alarm{ID: s[0], When: time.Unix(timestamp, 0)}
}
when, _ := time.Parse("2006-01-02 15:04:05", tm)
alarm := alarms[s[0]]
alarm.History = append(alarms[s[0]].History, AlarmHistory{
Type: typ,
When: when,
})
alarms[s[0]] = alarm
}
results := []Alarm{}
for _, v := range alarms {
results = append(results, v)
}
return c.JSON(http.StatusOK, results)
}
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/dgrijalva/jwt-go"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
)
func (app *App) PostExtractions(c echo.Context) error {
userNo := c.Get("user").(*jwt.Token).Claims.(*AuthClaims).UserNo
form, err := c.FormFile("file")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
src, err := form.Open()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
defer src.Close()
file, err := ioutil.TempFile("", "extraction")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
defer os.Remove(file.Name())
if _, err := io.Copy(file, src); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
file.Close()
db, err := sqlx.Connect("sqlite3", file.Name())
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not open db file")
}
defer db.Close()
tx, err := app.db.Beginx()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Unknown error")
}
res, _ := tx.Exec("INSERT INTO extractions (`owner`) VALUES (?)", userNo)
extNo, _ := res.LastInsertId()
rows, err := db.Queryx("SELECT * FROM calllog")
fmt.Println(err)
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
fmt.Println(vals)
_, err = tx.Exec("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
fmt.Println(err)
}
}
sql := `SELECT
a.packagename, a.name, a.version, a.wifiusage, a.cellularusage,
u.lasttimeused, u.totaltimeforeground
FROM AppInfo a JOIN AppUsageYear u ON a.packagename=u.packagename`
rows, err = db.Queryx(sql)
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO apps VALUES (?, ?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
rows, err = db.Queryx("SELECT mid, type, address, body, date FROM sms")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
rows, err = db.Queryx("SELECT PID, UID, PPID, STIME, TIME, CMD FROM process")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO processes VALUES (?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
/*alarms := map[string]Alarm{}
rows, _ = db.Queryx("SELECT * FROM alarm ORDER BY TIME")
for rows.Next() {
var tm string
var typ string
var detail string
rows.Scan(&tm, &typ, &detail)
detail = detail[strings.Index(detail, "{")+1 : strings.Index(detail, "}")]
s := strings.Split(detail, " ")
timestamp, _ := strconv.ParseInt(s[4], 10, 64)
timestamp /= 1000
if _, ok := alarms[s[0]]; !ok {
alarms[s[0]] = Alarm{ID: s[0], When: time.Unix(timestamp, 0)}
}
when, _ := time.Parse("2006-01-02 15:04:05", tm)
alarm := alarms[s[0]]
alarm.History = append(alarms[s[0]].History, AlarmHistory{
Type: typ,
When: when,
})
alarms[s[0]] = alarm
}
for _, v := range alarms {
tx.Exec("INSERT INTO alarms VALUES (?, ?, ?)", extNo, v.ID, v.When)
for _, h := range v.History {
tx.Exec("INSERT INTO alarm_histories VALUES (?, ?, ?, ?)", extNo, v.ID, h.Type, h.When)
}
}*/
tx.Commit()
return c.NoContent(http.StatusNoContent)
}
module mf-server
go 1.15
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/google/uuid v1.1.2
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/jmoiron/sqlx v1.2.0
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/echo/v4 v4.2.2
github.com/mattn/go-sqlite3 v1.9.0
golang.org/x/crypto v0.0.0-20201217014255-9d1352758620
)
This diff is collapsed. Click to expand it.
package main
import (
"log"
)
func main() {
config, err := LoadConfig("config.json")
if err != nil {
log.Fatal(err)
}
app := NewApp(config)
app.Serve()
}
package main
import (
"time"
)
type Time time.Time
func (t *Time) Scan(v interface{}) error {
p, err := time.Parse("2006-01-02 15:04:05", string(v.([]byte)))
*t = Time(p)
return err
}
func (t *Time) MarshalJSON() ([]byte, error) {
return time.Time(*t).MarshalJSON()
}
package main
import (
"context"
"net/http"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/go-sql-driver/mysql"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/sha3"
)
type User struct {
No uint64 `json:"no"`
ID string `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
ExpiredAt time.Time `json:"expired_at"`
}
func (app *App) PostUsers(c echo.Context) error {
body := struct {
ID string `json:"id"`
Password string `json:"password"`
Name string `json:"name"`
}{}
if c.Bind(&body) != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to parse request json")
}
hash := sha3.Sum256([]byte(body.Password))
res, err := app.db.Exec("INSERT INTO users (`id`, `password`, `name`) VALUES (?, ?, ?)", body.ID, hash[:], body.Name)
if err != nil {
if merr, ok := err.(*mysql.MySQLError); ok {
if merr.Number == 1062 {
return echo.NewHTTPError(http.StatusConflict, "Already registered")
}
}
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to register")
}
no, _ := res.LastInsertId()
return c.JSON(http.StatusOK, echo.Map{"user_no": no})
}
type AuthClaims struct {
UserNo uint64 `json:"user_no"`
jwt.StandardClaims
}
func (app *App) PostTokens(c echo.Context) error {
body := struct {
ID string `json:"id"`
Password string `json:"password"`
}{}
if c.Bind(&body) != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to parse request json")
}
hash := sha3.Sum256([]byte(body.Password))
rows, err := app.db.Query("SELECT `no` FROM users WHERE `id`=? AND `password`=?", body.ID, hash[:])
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to register")
}
if !rows.Next() {
return echo.NewHTTPError(http.StatusUnauthorized, "Login failed")
}
no := uint64(0)
rows.Scan(&no)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, AuthClaims{UserNo: no})
auth, err := token.SignedString([]byte(app.Config.TokenSecret))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Login failed")
}
return c.JSON(http.StatusOK, map[string]interface{}{"token": auth})
}
func (app *App) Authenticate(next func(http.ResponseWriter, *http.Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if len(auth) > 6 && strings.Index(auth, "Bearer ") == 0 {
token, err := jwt.ParseWithClaims(auth[7:], &AuthClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(app.Config.TokenSecret), nil
})
if err == nil {
claims := token.Claims.(*AuthClaims)
ctx := context.WithValue(r.Context(), PropUserNo, claims.UserNo)
next(w, r.WithContext(ctx))
return
}
}
WriteError(w, http.StatusUnauthorized, "Authorization failed")
})
}
package main
import (
"encoding/json"
"net/http"
)
func WriteJson(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func WriteError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(map[string]interface{}{"msg": message})
}