diff --git a/task3-backend/config/task3.yaml b/task3-backend/config/task3.yaml index eb0b5be..b336f7d 100644 --- a/task3-backend/config/task3.yaml +++ b/task3-backend/config/task3.yaml @@ -5,6 +5,9 @@ httpApi: task: addr: "0.0.0.0:31337" clients: 4 + letterPath: "./letter" + localPostboxPath: "./postbox" + remotePostboxPath: "/root/a-quest/deploy/task3/postbox" redis: addr: "localhost:6379" diff --git a/task3-backend/lib/conf.go b/task3-backend/lib/conf.go index 0beb647..2eecd89 100644 --- a/task3-backend/lib/conf.go +++ b/task3-backend/lib/conf.go @@ -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"` diff --git a/task3-backend/lib/server.go b/task3-backend/lib/server.go index da0fd50..1dfaa54 100644 --- a/task3-backend/lib/server.go +++ b/task3-backend/lib/server.go @@ -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 -} diff --git a/task3-backend/main.go b/task3-backend/main.go index 75ce009..2ec9ee5 100644 --- a/task3-backend/main.go +++ b/task3-backend/main.go @@ -109,13 +109,16 @@ func main() { } taskserv := &lib.Server{ - Alg: alg, - Pub: pubKey, - Priv: privKey, - Docker: docklient, - WinnersKey: Config.Redis.WinnersKey, - Redis: rediclient, - ClientsLimit: uint32(Config.Task.Clients), + Alg: alg, + Pub: pubKey, + Priv: privKey, + Docker: docklient, + WinnersKey: Config.Redis.WinnersKey, + Redis: rediclient, + ClientsLimit: uint32(Config.Task.Clients), + LetterPath: Config.Task.LetterPath, + LocalPostboxPath: Config.Task.LocalPostboxPath, + RemotePostboxPath: Config.Task.RemotePostboxPath, } go httpStart(apiserv)