-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from wrfly/develop
audit and share tty
- Loading branch information
Showing
21 changed files
with
519 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ vendor/ | |
# from gotty | ||
static | ||
js/node_modules/* | ||
|
||
# log file | ||
log/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.7 | ||
0.1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package audit | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path" | ||
"strings" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type LogOpts struct { | ||
Dir, ContainerID, ClientIP string | ||
} | ||
|
||
func LogTo(ctx context.Context, r io.Reader, opts LogOpts) { | ||
logDir := opts.Dir | ||
if !strings.HasPrefix(logDir, "/") { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
logrus.Errorf("audit get pwd error: %s", err) | ||
return | ||
} | ||
logDir = path.Join(pwd, logDir) | ||
} | ||
|
||
logDir = path.Join(logDir, opts.ContainerID[:12]) | ||
_, err := os.Stat(logDir) | ||
if os.IsNotExist(err) { | ||
logrus.Debugf("create dir %s", logDir) | ||
if err := os.MkdirAll(logDir, 0755); err != nil { | ||
logrus.Errorf("mkdir error: %s", err) | ||
return | ||
} | ||
} | ||
fPath := path.Join(logDir, fmt.Sprintf("%s-%d.log", | ||
strings.Split(opts.ClientIP, ":")[0], time.Now().Unix()), | ||
) | ||
|
||
f, err := os.Create(fPath) | ||
if err != nil { | ||
logrus.Errorf("audit create file [%s] error: %s", fPath, err) | ||
return | ||
} | ||
defer f.Close() | ||
|
||
buff := make([]byte, 2048) | ||
var start int64 | ||
for ctx.Err() == nil { | ||
n, err := r.Read(buff) | ||
if err != nil { | ||
if err == io.EOF { | ||
return | ||
} | ||
logrus.Errorf("audit read container error: %s", err) | ||
return | ||
} | ||
|
||
_, err = f.WriteAt(buff[:n], start) | ||
if err != nil { | ||
logrus.Errorf("audit write file error: %s", err) | ||
return | ||
} | ||
start += int64(n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.