|
|
@ -3,6 +3,7 @@ package common |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
|
|
|
@ -12,29 +13,16 @@ import ( |
|
|
|
"github.com/jszwec/csvutil" |
|
|
|
"github.com/jszwec/csvutil" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var utf8Bom = []byte{239, 187, 191} |
|
|
|
var ( |
|
|
|
|
|
|
|
utf8Bom = []byte{239, 187, 191} |
|
|
|
|
|
|
|
errWriteToFile = errors.New("write to file failed") |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func (b *Bookmarks) OutPutJson(browser, dir string) error { |
|
|
|
func (b *Bookmarks) OutPutJson(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "bookmark", "json") |
|
|
|
filename := utils.FormatFileName(dir, browser, "bookmark", "json") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
err := writeToJson(filename, b.bookmarks) |
|
|
|
defer func() { |
|
|
|
|
|
|
|
if err := file.Close(); err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Errorf("create file %s fail", filename) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w := new(bytes.Buffer) |
|
|
|
|
|
|
|
enc := json.NewEncoder(w) |
|
|
|
|
|
|
|
enc.SetEscapeHTML(false) |
|
|
|
|
|
|
|
enc.SetIndent("", "\t") |
|
|
|
|
|
|
|
enc.Encode(b.bookmarks) |
|
|
|
|
|
|
|
_, err = file.Write(w.Bytes()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error(err) |
|
|
|
log.Error(errWriteToFile) |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
fmt.Printf("%s Get %d bookmarks, filename is %s \n", log.Prefix, len(b.bookmarks), filename) |
|
|
|
fmt.Printf("%s Get %d bookmarks, filename is %s \n", log.Prefix, len(b.bookmarks), filename) |
|
|
@ -43,27 +31,9 @@ func (b *Bookmarks) OutPutJson(browser, dir string) error { |
|
|
|
|
|
|
|
|
|
|
|
func (h *History) OutPutJson(browser, dir string) error { |
|
|
|
func (h *History) OutPutJson(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "history", "json") |
|
|
|
filename := utils.FormatFileName(dir, browser, "history", "json") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
err := writeToJson(filename, h.history) |
|
|
|
defer func() { |
|
|
|
|
|
|
|
if err := file.Close(); err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Errorf("create file %s fail", filename) |
|
|
|
log.Error(errWriteToFile) |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w := new(bytes.Buffer) |
|
|
|
|
|
|
|
enc := json.NewEncoder(w) |
|
|
|
|
|
|
|
enc.SetEscapeHTML(false) |
|
|
|
|
|
|
|
enc.SetIndent("", "\t") |
|
|
|
|
|
|
|
err = enc.Encode(h.history) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Debug(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_, err = file.Write(w.Bytes()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
fmt.Printf("%s Get %d history, filename is %s \n", log.Prefix, len(h.history), filename) |
|
|
|
fmt.Printf("%s Get %d history, filename is %s \n", log.Prefix, len(h.history), filename) |
|
|
@ -72,27 +42,9 @@ func (h *History) OutPutJson(browser, dir string) error { |
|
|
|
|
|
|
|
|
|
|
|
func (l *Logins) OutPutJson(browser, dir string) error { |
|
|
|
func (l *Logins) OutPutJson(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "password", "json") |
|
|
|
filename := utils.FormatFileName(dir, browser, "password", "json") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
err := writeToJson(filename, l.logins) |
|
|
|
defer func() { |
|
|
|
|
|
|
|
if err := file.Close(); err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Errorf("create file %s fail", filename) |
|
|
|
log.Error(errWriteToFile) |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w := new(bytes.Buffer) |
|
|
|
|
|
|
|
enc := json.NewEncoder(w) |
|
|
|
|
|
|
|
enc.SetEscapeHTML(false) |
|
|
|
|
|
|
|
enc.SetIndent("", "\t") |
|
|
|
|
|
|
|
err = enc.Encode(l.logins) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Debug(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_, err = file.Write(w.Bytes()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
fmt.Printf("%s Get %d passwords, filename is %s \n", log.Prefix, len(l.logins), filename) |
|
|
|
fmt.Printf("%s Get %d passwords, filename is %s \n", log.Prefix, len(l.logins), filename) |
|
|
@ -101,119 +53,111 @@ func (l *Logins) OutPutJson(browser, dir string) error { |
|
|
|
|
|
|
|
|
|
|
|
func (c *Cookies) OutPutJson(browser, dir string) error { |
|
|
|
func (c *Cookies) OutPutJson(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "cookie", "json") |
|
|
|
filename := utils.FormatFileName(dir, browser, "cookie", "json") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
err := writeToJson(filename, c.cookies) |
|
|
|
defer func() { |
|
|
|
if err != nil { |
|
|
|
if err := file.Close(); err != nil { |
|
|
|
log.Error(errWriteToFile) |
|
|
|
log.Error(err) |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
fmt.Printf("%s Get %d cookies, filename is %s \n", log.Prefix, len(c.cookies), filename) |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func writeToJson(filename string, data interface{}) error { |
|
|
|
|
|
|
|
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Errorf("create file %s fail", filename) |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
defer func() { |
|
|
|
|
|
|
|
if err := f.Close(); err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
w := new(bytes.Buffer) |
|
|
|
w := new(bytes.Buffer) |
|
|
|
enc := json.NewEncoder(w) |
|
|
|
enc := json.NewEncoder(w) |
|
|
|
enc.SetEscapeHTML(false) |
|
|
|
enc.SetEscapeHTML(false) |
|
|
|
enc.SetIndent("", "\t") |
|
|
|
enc.SetIndent("", "\t") |
|
|
|
err = enc.Encode(c.cookies) |
|
|
|
err = enc.Encode(data) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Debug(err) |
|
|
|
log.Debug(err) |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
_, err = file.Write(w.Bytes()) |
|
|
|
_, err = f.Write(w.Bytes()) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error(err) |
|
|
|
log.Error(err) |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
fmt.Printf("%s Get %d cookies, filename is %s \n", log.Prefix, len(c.cookies), filename) |
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (b *Bookmarks) OutPutCsv(browser, dir string) error { |
|
|
|
func (b *Bookmarks) OutPutCsv(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "bookmark", "csv") |
|
|
|
filename := utils.FormatFileName(dir, browser, "bookmark", "csv") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
if err := writeToCsv(filename, b.bookmarks); err != nil { |
|
|
|
defer func() { |
|
|
|
log.Error(errWriteToFile) |
|
|
|
if err := file.Close(); err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Errorf("create file %s fail %s", filename, err) |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
file.Write(utf8Bom) |
|
|
|
|
|
|
|
data, err := csvutil.Marshal(b.bookmarks) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
file.Write(data) |
|
|
|
|
|
|
|
fmt.Printf("%s Get %d bookmarks, filename is %s \n", log.Prefix, len(b.bookmarks), filename) |
|
|
|
fmt.Printf("%s Get %d bookmarks, filename is %s \n", log.Prefix, len(b.bookmarks), filename) |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (h *History) OutPutCsv(browser, dir string) error { |
|
|
|
func (h *History) OutPutCsv(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "history", "csv") |
|
|
|
filename := utils.FormatFileName(dir, browser, "history", "csv") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
if err := writeToCsv(filename, h.history); err != nil { |
|
|
|
defer func() { |
|
|
|
log.Error(errWriteToFile) |
|
|
|
if err := file.Close(); err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Errorf("create file %s fail %s", filename, err) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
file.Write(utf8Bom) |
|
|
|
|
|
|
|
data, err := csvutil.Marshal(h.history) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
file.Write(data) |
|
|
|
|
|
|
|
fmt.Printf("%s Get %d history, filename is %s \n", log.Prefix, len(h.history), filename) |
|
|
|
fmt.Printf("%s Get %d history, filename is %s \n", log.Prefix, len(h.history), filename) |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (l *Logins) OutPutCsv(browser, dir string) error { |
|
|
|
func (l *Logins) OutPutCsv(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "password", "csv") |
|
|
|
filename := utils.FormatFileName(dir, browser, "password", "csv") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
if err := writeToCsv(filename, l.logins); err != nil { |
|
|
|
defer func() { |
|
|
|
log.Error(errWriteToFile) |
|
|
|
if err := file.Close(); err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Errorf("create file %s fail %s", filename, err) |
|
|
|
|
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
file.Write(utf8Bom) |
|
|
|
|
|
|
|
data, err := csvutil.Marshal(l.logins) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
file.Write(data) |
|
|
|
|
|
|
|
fmt.Printf("%s Get %d passwords, filename is %s \n", log.Prefix, len(l.logins), filename) |
|
|
|
fmt.Printf("%s Get %d passwords, filename is %s \n", log.Prefix, len(l.logins), filename) |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *Cookies) OutPutCsv(browser, dir string) error { |
|
|
|
func (c *Cookies) OutPutCsv(browser, dir string) error { |
|
|
|
filename := utils.FormatFileName(dir, browser, "cookie", "csv") |
|
|
|
filename := utils.FormatFileName(dir, browser, "cookie", "csv") |
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
var tempSlice []cookies |
|
|
|
|
|
|
|
for _, v := range c.cookies { |
|
|
|
|
|
|
|
tempSlice = append(tempSlice, v...) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if err := writeToCsv(filename, tempSlice); err != nil { |
|
|
|
|
|
|
|
log.Error(errWriteToFile) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fmt.Printf("%s Get %d cookies, filename is %s \n", log.Prefix, len(c.cookies), filename) |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func writeToCsv(filename string, data interface{}) error { |
|
|
|
|
|
|
|
var d []byte |
|
|
|
|
|
|
|
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_APPEND, 0644) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Errorf("create file %s fail %s", filename, err) |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
defer func() { |
|
|
|
defer func() { |
|
|
|
if err := file.Close(); err != nil { |
|
|
|
if err := f.Close(); err != nil { |
|
|
|
log.Error(err) |
|
|
|
log.Error(err) |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
_, err = f.Write(utf8Bom) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Errorf("create file %s fail", filename) |
|
|
|
log.Error(err) |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
var tempSlice []cookies |
|
|
|
d, err = csvutil.Marshal(data) |
|
|
|
for _, v := range c.cookies { |
|
|
|
if err != nil { |
|
|
|
tempSlice = append(tempSlice, v...) |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_, err = f.Write(d) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Error(err) |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
file.Write(utf8Bom) |
|
|
|
|
|
|
|
data, err := csvutil.Marshal(tempSlice) |
|
|
|
|
|
|
|
file.Write(data) |
|
|
|
|
|
|
|
fmt.Printf("%s Get %d cookies, filename is %s \n", log.Prefix, len(c.cookies), filename) |
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|