fastlink/web_api.go
2022-08-03 10:30:59 +08:00

459 lines
11 KiB
Go

package main
import (
"encoding/base64"
"encoding/json"
"github.com/beevik/guid"
"github.com/gorilla/websocket"
"io/ioutil"
"net/http"
"strings"
)
type WebApiRequest struct {
DeviceNo string `json:"deviceNo"`
Random string `json:"random"`
ClientNo string `json:"clientNo"`
UserName string `json:"userName"`
VerificationPassword string `json:"verificationPassword"`
}
type WebApiResponse struct {
TraceId interface{} `json:"traceId"`
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
Success bool `json:"success"`
Error bool `json:"error"`
}
type WebCheckData struct {
Pass bool `json:"pass"`
}
type DeviceVerificationTypeData struct {
DeviceId interface{} `json:"deviceId"`
VerificationType interface{} `json:"verificationType"`
}
type IdTransferData struct {
StreamingConfig StreamingConfig `json:"streamingConfig"`
StreamRecordId int `json:"streamRecordId"` //记录分析,没啥用,未实现相关功能
}
type LoginRequest struct {
Password string `json:"password"`
Phone string `json:"phone"`
}
type AppDeviceListData struct {
DeviceId int `json:"deviceId"`
DeviceNo string `json:"deviceNo"`
DeviceName string `json:"deviceName"`
ExpireTime interface{} `json:"expireTime"`
ExpireTimeStr interface{} `json:"expireTimeStr"`
OnlineState int `json:"onlineState"`
EnableConnect int `json:"enableConnect"`
ClientVersion interface{} `json:"clientVersion"`
}
type RegisterModel struct {
Code string `json:"code"`
Password string `json:"password"`
Password2 string `json:"password2"`
Phone string `json:"phone"`
UserName string `json:"userName"`
}
func checkHandler(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var model *WebApiRequest
json.Unmarshal(data, &model)
decodeString, _ := base64.StdEncoding.DecodeString(model.DeviceNo)
model.DeviceNo = string(decodeString)
result := fastLinkService.CheckWebLogin(model.DeviceNo, model.Random)
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: &WebCheckData{
Pass: true,
},
Success: true,
Error: false,
}
switch result {
case 1:
resp.Data = nil
resp.Error = true
resp.Success = false
resp.Message = "远程主机没有分享"
resp.Code = 500
case 2:
resp.Data = nil
resp.Error = true
resp.Success = false
resp.Message = "要连接的主机不存在"
resp.Code = 500
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
func deviceVerificationTypeHandler(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var model *WebApiRequest
json.Unmarshal(data, &model)
device := fastLinkService.GetDeviceByNo(model.DeviceNo)
vData := &DeviceVerificationTypeData{
DeviceId: nil,
VerificationType: nil,
}
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: vData,
Success: true,
Error: false,
}
if device != nil {
vData.DeviceId = device.DeviceId
vData.VerificationType = device.VerificationType
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
func idTransferHandler(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var model *WebApiRequest
json.Unmarshal(data, &model)
device := fastLinkService.GetDeviceByNo(model.DeviceNo)
if device == nil {
resp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "设备不存在",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
} else {
if device.Online && device.Visible {
if device.VerificationType == 2 && device.TwoStepPass != "" && device.TwoStepPass != model.VerificationPassword {
resp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "访问密码不正确",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
return
}
streamToken := GenShortGUID()
if model.UserName == "" {
model.UserName = "WEB客户端"
}
fastLinkService.PrepareSignalSession(model.ClientNo, device.DeviceId, device.DeviceNo, streamToken, model.UserName)
targetRemoteControlResp := &RemoteStartRemoteControlResponse{
Id: guid.New().String(),
Method: "SetStreamingConfig",
Params: RemoteStreamingConfig{
ClientId: model.ClientNo,
SignalServer: globalConfig.SignalServer,
StunAddrs: globalConfig.StunServers,
Token: streamToken,
TurnAddrs: globalConfig.TurnServers,
},
}
remoteSession := fastLinkService.GetSession(device.DeviceNo, CLIENT_TYPE_FASTLINK_SERVICE)
remoteSession.WsConn.WriteMessage(websocket.TextMessage, makeMessageResponse(device.DeviceNo+"_service", targetRemoteControlResp))
//推送UI提示
notifyOnlineUserChange(device.DeviceNo)
idTransferData := IdTransferData{
StreamingConfig: StreamingConfig{
ClientId: model.ClientNo,
ClientIds: []string{model.ClientNo},
ServiceId: model.DeviceNo,
SignalServer: globalConfig.SignalServer,
StunAddrs: globalConfig.StunServers,
Token: streamToken,
TurnAddrs: globalConfig.TurnServers,
},
StreamRecordId: 0,
}
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: idTransferData,
Success: true,
Error: false,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
} else {
resp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "远程主机不在线",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
}
}
func closeHandler(w http.ResponseWriter, r *http.Request) {
//记录埋点用,未实现,直接返回
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: nil,
Success: true,
Error: false,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
func loginForAppHandler(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var model *LoginRequest
json.Unmarshal(data, &model)
success := fastLinkService.CheckUser(model.Phone, model.Password)
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: nil,
Success: true,
Error: false,
}
if !success {
resp.Success = false
resp.Error = true
resp.Code = 500
resp.Message = "用户名或密码不正确!"
}
token := fastLinkService.NewToken(model.Phone, TOKEN_TYPE_APP)
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: token,
})
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
func appListDeviceHandler(w http.ResponseWriter, r *http.Request) {
cookie := r.Header.Get("Cookie")
user := ""
token := ""
if len(cookie) > 7 {
index := strings.Index(cookie, "token=")
token = cookie[index+6:]
user = fastLinkService.GetUserByToken(token)
}
if user == "" {
errResp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "用户未登录",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(errResp)
w.Write(marshal)
return
}
fastLinkService.RenewToken(token)
devices := fastLinkService.GetDeviceByToken(token)
var data []*AppDeviceListData
for _, device := range devices {
t := &AppDeviceListData{
DeviceId: device.DeviceId,
DeviceNo: device.DeviceNo,
DeviceName: device.DeviceName,
ExpireTime: nil,
ExpireTimeStr: nil,
ClientVersion: nil,
}
if device.CustomName != "" {
t.DeviceName = device.CustomName
}
if device.Online {
t.OnlineState = 1
} else {
t.OnlineState = 0
}
if device.Visible {
t.EnableConnect = 1
} else {
t.EnableConnect = 0
}
data = append(data, t)
}
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: data,
Success: true,
Error: false,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
func logoutHandler(w http.ResponseWriter, r *http.Request) {
cookie := r.Header.Get("Cookie")
user := ""
token := ""
if len(cookie) > 7 {
index := strings.Index(cookie, "token=")
token = cookie[index+6:]
user = fastLinkService.GetUserByToken(token)
}
if user == "" {
errResp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "用户未登录",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(errResp)
w.Write(marshal)
return
}
fastLinkService.RemoveToken(token)
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: nil,
Success: true,
Error: false,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
func registerHandler(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
var model *RegisterModel
json.Unmarshal(data, &model)
if model.UserName == "" || model.Phone == "" {
errResp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "用户名不能为空",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(errResp)
w.Write(marshal)
return
}
if model.Password == "" {
errResp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "密码不能为空",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(errResp)
w.Write(marshal)
return
}
if model.Password != model.Password2 {
errResp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "两次密码输入不一致",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(errResp)
w.Write(marshal)
return
}
if model.Password != model.Password2 {
errResp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "两次密码输入不一致",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(errResp)
w.Write(marshal)
return
}
exist := fastLinkService.CheckUserExist(model.UserName)
if exist {
errResp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "此手机号已被其他用户绑定!",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(errResp)
w.Write(marshal)
return
}
fastLinkService.AddUser(model.UserName, model.Password)
resp := WebApiResponse{
TraceId: nil,
Code: 200,
Message: "",
Data: nil,
Success: true,
Error: false,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}
func sendCodeHandler(w http.ResponseWriter, r *http.Request) {
resp := WebApiResponse{
TraceId: nil,
Code: 500,
Message: "私服未实现发送验证码,输入任意验证码即可",
Data: nil,
Success: false,
Error: true,
}
marshal, _ := json.Marshal(resp)
w.Write(marshal)
}