mirror of
https://github.com/mainnika/a-quest.git
synced 2026-05-24 00:33:36 +00:00
create letters postbox
This commit is contained in:
@@ -11,8 +11,11 @@ type AppConfig struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
} `mapstructure:"httpApi"`
|
||||
Task struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
Clients int `mapstructure:"clients"`
|
||||
Addr string `mapstructure:"addr"`
|
||||
Clients int `mapstructure:"clients"`
|
||||
LetterPath string `mapstructure:"letterPath"`
|
||||
LocalPostboxPath string `mapstructure:"localPostboxPath"`
|
||||
RemotePostboxPath string `mapstructure:"remotePostboxPath"`
|
||||
} `mapstructure:"task"`
|
||||
Redis struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
|
||||
+33
-31
@@ -26,7 +26,9 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
LetterPath string
|
||||
LetterPath string
|
||||
LocalPostboxPath string
|
||||
RemotePostboxPath string
|
||||
|
||||
Alg string
|
||||
Pub *ecdsa.PublicKey
|
||||
@@ -179,11 +181,12 @@ func (s *Server) readKey(conn net.Conn) (key string, err error) {
|
||||
|
||||
func (s *Server) createJail(now int64, claims *jwtgo.StandardClaims) (id string, err error) {
|
||||
|
||||
letterPath, letterFullPath, err := createLetterProtected(s.LetterPath)
|
||||
letterPath, _, err := s.createLetter()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
remoteLetterPath := path.Join(s.RemotePostboxPath, letterPath)
|
||||
name := fmt.Sprintf("%d-%s", now, claims.Id)
|
||||
cfg := &container.Config{
|
||||
NetworkDisabled: true,
|
||||
@@ -198,7 +201,7 @@ func (s *Server) createJail(now int64, claims *jwtgo.StandardClaims) (id string,
|
||||
}
|
||||
letter := mount.Mount{
|
||||
Type: mount.TypeBind,
|
||||
Source: letterFullPath,
|
||||
Source: remoteLetterPath,
|
||||
Target: fmt.Sprintf("/%s", letterPath),
|
||||
}
|
||||
hcfg := &container.HostConfig{
|
||||
@@ -280,6 +283,33 @@ Proxying:
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Server) createLetter() (letterPath string, localFullPath string, err error) {
|
||||
|
||||
source, err := os.Open(s.LetterPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer source.Close()
|
||||
|
||||
letterPath = fmt.Sprintf("%s.letter", uuid.New().String())
|
||||
localFullPath = path.Join(s.LocalPostboxPath, letterPath)
|
||||
|
||||
letter, err := os.OpenFile(localFullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0000)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer letter.Close()
|
||||
|
||||
_, err = io.Copy(letter, source)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_ = unix.Sync()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func createProxyChan(conn net.Conn, closed *uint32) (out chan []byte) {
|
||||
|
||||
out = make(chan []byte)
|
||||
@@ -316,31 +346,3 @@ func createProxyChan(conn net.Conn, closed *uint32) (out chan []byte) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func createLetterProtected(sourcePath string) (letterPath string, letterFullPath string, err error) {
|
||||
|
||||
source, err := os.Open(sourcePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer source.Close()
|
||||
|
||||
tmpPath := os.TempDir()
|
||||
letterPath = fmt.Sprintf("%s.letter", uuid.New().String())
|
||||
letterFullPath = path.Join(tmpPath, letterPath)
|
||||
|
||||
letter, err := os.OpenFile(letterFullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0000)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer letter.Close()
|
||||
|
||||
_, err = io.Copy(letter, source)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_ = unix.Sync()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user