send proto to url and basicauth support
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
fcdb733839
commit
c64305953a
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module git.0cd.xyz/michael/gtools
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
git.0cd.xyz/michael/request v0.0.6
|
||||||
github.com/OneOfOne/xxhash v1.2.8
|
github.com/OneOfOne/xxhash v1.2.8
|
||||||
github.com/gorilla/csrf v1.7.0
|
github.com/gorilla/csrf v1.7.0
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package nettools
|
package nettools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"log"
|
"log"
|
||||||
"math/big"
|
"math/big"
|
||||||
@ -8,6 +9,12 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func BasicAuth(username, password string) map[string]string {
|
||||||
|
return map[string]string{
|
||||||
|
"Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IP returns IP address from requester
|
// IP returns IP address from requester
|
||||||
func IP(r *http.Request) (net.IP, error) {
|
func IP(r *http.Request) (net.IP, error) {
|
||||||
realip := r.Header.Get("x-forwarded-for")
|
realip := r.Header.Get("x-forwarded-for")
|
||||||
|
38
pb/proto.go
38
pb/proto.go
@ -1,9 +1,13 @@
|
|||||||
package pb
|
package pb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.0cd.xyz/michael/request"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"google.golang.org/protobuf/reflect/protoreflect"
|
"google.golang.org/protobuf/reflect/protoreflect"
|
||||||
)
|
)
|
||||||
@ -31,3 +35,37 @@ func WriteFile(path string, message protoreflect.ProtoMessage, perm fs.FileMode)
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendFile protocol buffer as multipart file to http address
|
||||||
|
func SendFile(filename, url string, headers map[string]string, message protoreflect.ProtoMessage) (req *request.Response, err error) {
|
||||||
|
body, err := proto.Marshal(message)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
writer := multipart.NewWriter(buf)
|
||||||
|
part, err := writer.CreateFormFile("file", filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
part.Write(body)
|
||||||
|
for key, val := range map[string]string{
|
||||||
|
"type": "upload",
|
||||||
|
"synchronous": "true",
|
||||||
|
} {
|
||||||
|
writer.WriteField(key, val)
|
||||||
|
}
|
||||||
|
writer.Close()
|
||||||
|
|
||||||
|
headers["Content-Length"] = strconv.Itoa(buf.Len())
|
||||||
|
headers["Content-Type"] = writer.FormDataContentType()
|
||||||
|
|
||||||
|
req, err = request.Post(url,
|
||||||
|
headers, buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user