Skip to content

Commit

Permalink
fix: incomplete downloads cannot be resumed
Browse files Browse the repository at this point in the history
  • Loading branch information
jm33-m0 committed Jan 19, 2024
1 parent c646012 commit bbd57f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
38 changes: 8 additions & 30 deletions core/cmd/cc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,6 @@ func readJSONConfig(filename string) (err error) {
return emp3r0r_data.ReadJSONConfig(jsonData, cc.RuntimeConfig)
}

// unlock_downloads if there are incomplete file downloads that are "locked", unlock them
// unless CC is actually running/downloading
func unlock_downloads() bool {
// is cc currently running?
if tun.IsPortOpen("127.0.0.1", cc.RuntimeConfig.CCPort) {
return false
}

// unlock downloads
files, err := os.ReadDir(cc.FileGetDir)
if err != nil {
return true
}
for _, f := range files {
if strings.HasSuffix(f.Name(), ".lock") {
err = os.Remove(cc.FileGetDir + f.Name())
if err != nil {
cc.CliFatalError("Remove %s: %v", f.Name(), err)
}
}
}

return true
}

// re-generate a random magic string for this CC session
func init_magic_str() {
default_magic_str := emp3r0r_data.OneTimeMagicBytes
Expand All @@ -77,15 +52,13 @@ func init_magic_str() {
}

func main() {
var err error

// cleanup or abort
if !unlock_downloads() {
// abort if CC is already running
if cc.IsCCRunning() {
cc.CliFatalError("CC is already running")
}

// set up dirs
err = cc.DirSetup()
err := cc.DirSetup()
if err != nil {
cc.CliFatalError("DirSetup: %v", err)
}
Expand Down Expand Up @@ -131,6 +104,11 @@ func main() {
cc.CliFatalError("SSHRemoteFwdServer: %v", err)
}
} else {
// unlock downloads
err = cc.UnlockDownloads()
if err != nil {
cc.CliPrintWarning("UnlockDownloads: %v", err)
}
go cc.TLSServer()
go cc.ShadowsocksServer()
go cc.InitModules()
Expand Down
30 changes: 30 additions & 0 deletions core/lib/cc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cavaliergopher/grab/v3"
"github.com/google/uuid"
emp3r0r_data "github.com/jm33-m0/emp3r0r/core/lib/data"
"github.com/jm33-m0/emp3r0r/core/lib/tun"
"github.com/jm33-m0/emp3r0r/core/lib/util"
)

Expand Down Expand Up @@ -263,3 +264,32 @@ func GetDateTime() (datetime string) {

return
}

// IsCCRunning check if CC is already running
func IsCCRunning() bool {
// it is running if we can connect to it
if tun.IsPortOpen("127.0.0.1", RuntimeConfig.CCPort) {
return true
}
return false
}

// UnlockDownloads if there are incomplete file downloads that are "locked", unlock them
// unless CC is actually running/downloading
func UnlockDownloads() error {
// unlock downloads
files, err := os.ReadDir(FileGetDir)
if err != nil {
return err
}
for _, f := range files {
if strings.HasSuffix(f.Name(), ".lock") {
err = os.Remove(FileGetDir + f.Name())
if err != nil {
return fmt.Errorf("Remove %s: %v", f.Name(), err)
}
}
}

return nil
}

0 comments on commit bbd57f9

Please sign in to comment.