191 lines
5 KiB
Go
191 lines
5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"PD2/internal/protocol"
|
|
"bytes"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
const baseURL = "https://127.0.0.1:8080"
|
|
|
|
func getHTTPClient(tlsConfig *tls.Config) *http.Client{
|
|
transport := &http.Transport{TLSClientConfig: tlsConfig}
|
|
return &http.Client{Transport: transport}
|
|
}
|
|
|
|
func forwardStoreUserCert(tlsConfig *tls.Config,uid string,storeUserCertificate protocol.StoreUserCert) (int,[]byte,error) {
|
|
client := getHTTPClient(tlsConfig)
|
|
|
|
// Parse the base URL
|
|
parsedURL, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
|
}
|
|
parsedURL = parsedURL.JoinPath("certs")
|
|
parsedURL = parsedURL.JoinPath(uid)
|
|
|
|
jsonData, err := json.Marshal(storeUserCertificate)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error marshaling JSON: %v", err)
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error creating request: %v", err)
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error making request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Read response
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error reading response body: %v", err)
|
|
}
|
|
|
|
return resp.StatusCode,body,nil
|
|
}
|
|
|
|
func forwardGetMessage(tlsConfig *tls.Config,uid string,num string) (int,[]byte,error) {
|
|
client := getHTTPClient(tlsConfig)
|
|
|
|
// Parse the base URL
|
|
parsedURL, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
|
}
|
|
parsedURL = parsedURL.JoinPath("message")
|
|
parsedURL = parsedURL.JoinPath(uid)
|
|
parsedURL = parsedURL.JoinPath(num)
|
|
|
|
|
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error creating request: %v", err)
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error making request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Read response
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error reading response body: %v", err)
|
|
}
|
|
|
|
return resp.StatusCode,body,nil
|
|
}
|
|
|
|
func forwardGetUnreadMsgsInfo(tlsConfig *tls.Config,uid string,page string,pagesize string) (int,[]byte,error) {
|
|
client := getHTTPClient(tlsConfig)
|
|
|
|
// Parse the base URL
|
|
parsedURL, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
|
}
|
|
parsedURL = parsedURL.JoinPath("queue")
|
|
parsedURL = parsedURL.JoinPath(uid)
|
|
|
|
query := parsedURL.Query()
|
|
query.Set("page", page)
|
|
query.Set("pagesize", pagesize)
|
|
parsedURL.RawQuery = query.Encode()
|
|
|
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error creating request: %v", err)
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error making request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Read response
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error reading response body: %v", err)
|
|
}
|
|
|
|
return resp.StatusCode,body,nil
|
|
}
|
|
|
|
func forwardGetUserCert(tlsConfig *tls.Config,uid string) (int,[]byte,error) {
|
|
client := getHTTPClient(tlsConfig)
|
|
|
|
// Parse the base URL
|
|
parsedURL, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
|
}
|
|
parsedURL = parsedURL.JoinPath("cert")
|
|
parsedURL = parsedURL.JoinPath(uid)
|
|
|
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error creating request: %v", err)
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error making request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Read response
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error reading response body: %v", err)
|
|
}
|
|
|
|
return resp.StatusCode,body,nil
|
|
}
|
|
|
|
func forwardSendMessage(tlsConfig *tls.Config,uid string,sendMsg protocol.SendMsg) (int,[]byte,error) {
|
|
client := getHTTPClient(tlsConfig)
|
|
|
|
// Parse the base URL
|
|
parsedURL, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
|
}
|
|
parsedURL = parsedURL.JoinPath("message")
|
|
parsedURL = parsedURL.JoinPath(uid)
|
|
|
|
jsonData, err := json.Marshal(sendMsg)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error marshaling JSON: %v", err)
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error creating request: %v", err)
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error making request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Read response
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return 0,nil,fmt.Errorf("error reading response body: %v", err)
|
|
}
|
|
|
|
return resp.StatusCode,body,nil
|
|
}
|