create letters postbox

main
Nikita Tokarchuk 4 years ago
parent fa8509a441
commit ba0240aa84
  1. 3
      task3-backend/config/task3.yaml
  2. 7
      task3-backend/lib/conf.go
  3. 64
      task3-backend/lib/server.go
  4. 17
      task3-backend/main.go

@ -5,6 +5,9 @@ httpApi:
task: task:
addr: "0.0.0.0:31337" addr: "0.0.0.0:31337"
clients: 4 clients: 4
letterPath: "./letter"
localPostboxPath: "./postbox"
remotePostboxPath: "/root/a-quest/deploy/task3/postbox"
redis: redis:
addr: "localhost:6379" addr: "localhost:6379"

@ -11,8 +11,11 @@ type AppConfig struct {
Addr string `mapstructure:"addr"` Addr string `mapstructure:"addr"`
} `mapstructure:"httpApi"` } `mapstructure:"httpApi"`
Task struct { Task struct {
Addr string `mapstructure:"addr"` Addr string `mapstructure:"addr"`
Clients int `mapstructure:"clients"` Clients int `mapstructure:"clients"`
LetterPath string `mapstructure:"letterPath"`
LocalPostboxPath string `mapstructure:"localPostboxPath"`
RemotePostboxPath string `mapstructure:"remotePostboxPath"`
} `mapstructure:"task"` } `mapstructure:"task"`
Redis struct { Redis struct {
Addr string `mapstructure:"addr"` Addr string `mapstructure:"addr"`

@ -26,7 +26,9 @@ import (
) )
type Server struct { type Server struct {
LetterPath string LetterPath string
LocalPostboxPath string
RemotePostboxPath string
Alg string Alg string
Pub *ecdsa.PublicKey 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) { 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 { if err != nil {
return return
} }
remoteLetterPath := path.Join(s.RemotePostboxPath, letterPath)
name := fmt.Sprintf("%d-%s", now, claims.Id) name := fmt.Sprintf("%d-%s", now, claims.Id)
cfg := &container.Config{ cfg := &container.Config{
NetworkDisabled: true, NetworkDisabled: true,
@ -198,7 +201,7 @@ func (s *Server) createJail(now int64, claims *jwtgo.StandardClaims) (id string,
} }
letter := mount.Mount{ letter := mount.Mount{
Type: mount.TypeBind, Type: mount.TypeBind,
Source: letterFullPath, Source: remoteLetterPath,
Target: fmt.Sprintf("/%s", letterPath), Target: fmt.Sprintf("/%s", letterPath),
} }
hcfg := &container.HostConfig{ hcfg := &container.HostConfig{
@ -280,6 +283,33 @@ Proxying:
return 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) { func createProxyChan(conn net.Conn, closed *uint32) (out chan []byte) {
out = make(chan []byte) out = make(chan []byte)
@ -316,31 +346,3 @@ func createProxyChan(conn net.Conn, closed *uint32) (out chan []byte) {
return 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
}

@ -109,13 +109,16 @@ func main() {
} }
taskserv := &lib.Server{ taskserv := &lib.Server{
Alg: alg, Alg: alg,
Pub: pubKey, Pub: pubKey,
Priv: privKey, Priv: privKey,
Docker: docklient, Docker: docklient,
WinnersKey: Config.Redis.WinnersKey, WinnersKey: Config.Redis.WinnersKey,
Redis: rediclient, Redis: rediclient,
ClientsLimit: uint32(Config.Task.Clients), ClientsLimit: uint32(Config.Task.Clients),
LetterPath: Config.Task.LetterPath,
LocalPostboxPath: Config.Task.LocalPostboxPath,
RemotePostboxPath: Config.Task.RemotePostboxPath,
} }
go httpStart(apiserv) go httpStart(apiserv)

Loading…
Cancel
Save