create letters postbox

main
Nikita Tokarchuk 5 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:
addr: "0.0.0.0:31337"
clients: 4
letterPath: "./letter"
localPostboxPath: "./postbox"
remotePostboxPath: "/root/a-quest/deploy/task3/postbox"
redis:
addr: "localhost:6379"

@ -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"`

@ -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
}

@ -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)

Loading…
Cancel
Save