-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
220 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package afc | ||
|
||
// #cgo pkg-config: libimobiledevice-1.0 | ||
// #include <stdlib.h> | ||
// #include <libimobiledevice/afc.h> | ||
// int afc_length(char **arr) | ||
// { | ||
// int length = 0; | ||
// int k = 0; | ||
// for (k = 0; arr[k] != NULL; k++) { | ||
// length = length + 1; | ||
// } | ||
// return length; | ||
// } | ||
import "C" | ||
import ( | ||
"unsafe" | ||
|
||
"github.com/nowsecure/goidevice/idevice" | ||
"github.com/nowsecure/goidevice/lockdown" | ||
) | ||
|
||
type AFC struct { | ||
a C.afc_client_t | ||
} | ||
|
||
func NewClient(device idevice.Device, svc *lockdown.Service) (*AFC, error) { | ||
var a C.afc_client_t | ||
err := resultToError( | ||
C.afc_client_new( | ||
(C.idevice_t)(idevice.GetPointer(device)), | ||
(C.lockdownd_service_descriptor_t)(svc.GetDescriptor()), | ||
&a, | ||
), | ||
) | ||
return &AFC{a}, err | ||
} | ||
|
||
func (a *AFC) WalkDirectory(path string) ([]SourceFile, error) { | ||
dir, err := a.ReadDirectory(path) | ||
if err != nil { | ||
return []SourceFile{}, err | ||
} | ||
|
||
files := []SourceFile{} | ||
for _, f := range dir { | ||
if f.format == "S_IFDIR" { | ||
childDir, err := a.WalkDirectory(f.Name) | ||
if err != nil { | ||
return []SourceFile{}, err | ||
} | ||
files = append(files, childDir...) | ||
continue | ||
} | ||
files = append(files, f) | ||
} | ||
return files, nil | ||
} | ||
|
||
type SourceFile struct { | ||
Name string | ||
|
||
format string | ||
} | ||
|
||
func (a *AFC) ReadDirectory(path string) ([]SourceFile, error) { | ||
var directoryC **C.char | ||
defer C.afc_dictionary_free(directoryC) | ||
|
||
sourceFiles := []SourceFile{} | ||
pathC := C.CString(path) | ||
defer C.free(unsafe.Pointer(pathC)) | ||
|
||
err := resultToError(C.afc_read_directory(a.a, pathC, &directoryC)) | ||
if err != nil { | ||
return []SourceFile{}, err | ||
} | ||
|
||
directory := unsafe.Slice(directoryC, C.afc_length(directoryC)) | ||
for i := range directory { | ||
file := SourceFile{ | ||
Name: C.GoString(directory[i]), | ||
} | ||
if file.Name == ".." || file.Name == "." { | ||
continue | ||
} | ||
|
||
var fileInfoC **C.char | ||
C.afc_get_file_info(a.a, directory[i], &fileInfoC) | ||
if fileInfoC != nil { | ||
fileInfo := unsafe.Slice(fileInfoC, C.afc_length(fileInfoC)) | ||
|
||
for j := 0; j < len(fileInfo); j += 2 { | ||
if C.GoString(fileInfo[j]) == "st_ifmt" { | ||
file.format = C.GoString(fileInfo[j+1]) | ||
} | ||
} | ||
} | ||
C.afc_dictionary_free(fileInfoC) | ||
sourceFiles = append(sourceFiles, file) | ||
} | ||
return sourceFiles, nil | ||
} |
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,20 @@ | ||
package afc | ||
|
||
// #cgo pkg-config: libimobiledevice-1.0 | ||
// #include <stdlib.h> | ||
// #include <libimobiledevice/afc.h> | ||
import "C" | ||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
func resultToError(result C.afc_error_t) error { | ||
switch result { | ||
case 0: | ||
return nil | ||
default: | ||
fmt.Println(result) | ||
return errors.New("unknown") | ||
} | ||
} |
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,30 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/nowsecure/goidevice/afc" | ||
"github.com/nowsecure/goidevice/idevice" | ||
"github.com/nowsecure/goidevice/lockdown" | ||
) | ||
|
||
func main() { | ||
device, err := idevice.New(os.Args[1]) | ||
device, err := idevice.New("bd133240a37062e545bbbbf664f0011c9f45895d") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
lock, err := lockdown.NewClientWithHandshake(device, "thingy") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
client, err := lock.StartService(device, lockdown.CRASH_REPORT_MOVER_SERVICE) | ||
client, err := lock.StartServiceClient(device, lockdown.CRASH_REPORT_MOVER_SERVICE) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer client.Free() | ||
err = client.ReadPing() | ||
if err != nil { | ||
log.Fatal(err) | ||
} else { | ||
log.Println("yay we did it") | ||
} | ||
|
||
service, err := lock.StartService(device, lockdown.CRASH_REPORT_COPY_MOBILE_SERVICE) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer service.Free() | ||
|
||
afc, err := afc.NewClient(device, service) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
k, _ := afc.WalkDirectory(".") | ||
for _, v := range k { | ||
fmt.Println(v) | ||
} | ||
|
||
log.Println("yay we did it") | ||
} |
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,7 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func Test_ke(t *testing.T) { | ||
main() | ||
} |