Skip to content

Commit

Permalink
add constructor for nas; rename sessionid to session
Browse files Browse the repository at this point in the history
Signed-off-by: Rumen Vasilev <git@rumenvasilev.com>
  • Loading branch information
rumenvasilev committed Nov 16, 2023
1 parent 7b5a27e commit ebd9eea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
22 changes: 11 additions & 11 deletions auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ import (
)

const (
address = "http://fritz.box"
Address = "http://fritz.box"
loginPath = "login_sid.lua?version=2"
)

type SessionID string
type Session string

func (s *SessionID) String() string {
func (s *Session) String() string {
return string(*s)
}

// Auth will authenticate to the target FRITZOS device using default address
// and will return either session id, either error.
func Auth(username, password string) (*SessionID, error) {
return AuthWithAddress(address, username, password)
func Auth(username, password string) (*Session, error) {
return AuthWithAddress(Address, username, password)
}

func AuthWithAddress(address, username, password string) (*SessionID, error) {
func AuthWithAddress(address, username, password string) (*Session, error) {
challenge, err := getChallengeString(address)
if err != nil {
return nil, err
Expand All @@ -50,11 +50,11 @@ func AuthWithAddress(address, username, password string) (*SessionID, error) {
}

// Close will logout from the authenticated device
func Close(sid *SessionID) error {
return CloseWithAddress(address, sid)
func Close(sid *Session) error {
return CloseWithAddress(Address, sid)
}

func CloseWithAddress(address string, sid *SessionID) error {
func CloseWithAddress(address string, sid *Session) error {
fullAddress := fmt.Sprintf("%s/%s", address, loginPath)
data := url.Values{}
data.Set("logout", sid.String())
Expand Down Expand Up @@ -178,7 +178,7 @@ func calculateMD5Response(challenge, password string) string {

}

func authenticate(address, challenge, username string) (*SessionID, error) {
func authenticate(address, challenge, username string) (*Session, error) {
loginURL := fmt.Sprintf("%s/%s", address, loginPath)
v := url.Values{}
v.Set("username", username)
Expand All @@ -205,6 +205,6 @@ func authenticate(address, challenge, username string) (*SessionID, error) {
return nil, errors.New("login failed, session id is wrong")
}

sid := SessionID(session.SID)
sid := Session(session.SID)
return &sid, nil
}
25 changes: 11 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ func main() {
username := ""
password := ""

sessionID, err := auth.Auth(username, password)
defer auth.Close(sessionID)
Session, err := auth.Auth(username, password)
defer auth.Close(Session)
if err != nil {
log.Fatalln(err)
}

log.Println("Login successful! Session ID", sessionID)
log.Println("Login successful! Session ID", Session)

// List all files at the root
// res, err := ListDirectory(sessionID)
// res, err := ListDirectory(Session)
// if err != nil {
// log.Fatalln(err)
// }
Expand All @@ -31,7 +31,7 @@ func main() {
// params := make(map[string]string)
// params["path"] = "/Bilder"
// params["limit"] = "100"
// res, err := ListDirectoryWithParams(sessionID, params)
// res, err := ListDirectoryWithParams(Session, params)
// if err != nil {
// log.Fatalln(err)
// }
Expand All @@ -57,7 +57,7 @@ func main() {
// }

// Get specific object from the NAS
// d, err := GetFile(sessionID, "/Bilder/FRITZ-Picture.jpg")
// d, err := GetFile(Session, "/Bilder/FRITZ-Picture.jpg")
// if err != nil {
// log.Fatalln(err)
// }
Expand All @@ -75,7 +75,7 @@ func main() {
// if err != nil {
// log.Fatalln(err)
// }
// r, err := PutFile(sessionID, "/Dokumente/2022-0006_Anmeldung_fr_den_Schwimmkurs_Einverstndniserklrung.pdf", data)
// r, err := PutFile(Session, "/Dokumente/2022-0006_Anmeldung_fr_den_Schwimmkurs_Einverstndniserklrung.pdf", data)
// if err != nil {
// log.Fatalln("failed uploading the file, %w", err)
// }
Expand All @@ -86,33 +86,30 @@ func main() {
// fmt.Println(string(s))

// Rename File
// r, err := RenameFile(sessionID, "/Dokumente/blabla.pdf", "2022-0006-baba.pdf")
// r, err := RenameFile(Session, "/Dokumente/blabla.pdf", "2022-0006-baba.pdf")
// if err != nil {
// log.Fatalf("Rename failed, %v", err)
// }
// s, _ := json.Marshal(r)
// fmt.Println(string(s))

// Delete File
// r, err := DeleteFile(sessionID, "/Dokumente/2022-0006-baba.pdf")
// r, err := DeleteFile(Session, "/Dokumente/2022-0006-baba.pdf")
// if err != nil {
// log.Fatalf("Rename failed, %v", err)
// }
// s, _ := json.Marshal(r)
// fmt.Println(string(s))

// Move File
// n, err := MoveFile(sessionID, "/2022-0006_Anmeldung_fr_den_Schwimmkurs_Einverstndniserklrung.pdf", "/Dokumente")
// n, err := MoveFile(Session, "/2022-0006_Anmeldung_fr_den_Schwimmkurs_Einverstndniserklrung.pdf", "/Dokumente")
// if err != nil {
// log.Fatalf("Move failed, %v", err)
// }
// fmt.Println(n)

// Create Dir
n := &nas.NAS{
SessionID: sessionID,
Address: "http://fritz.box",
}
n := nas.New(Session).WithAddress("http://fritz.box")

r, err := n.CreateDir("blabla", "/Dokumente")
if err != nil {
Expand Down
50 changes: 31 additions & 19 deletions nas/nas.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const (
)

type NAS struct {
*auth.SessionID
Address string
session *auth.Session
address string
}

type BrowseResponse struct {
Expand Down Expand Up @@ -90,6 +90,18 @@ func (p *Timestamp) UnmarshalJSON(bytes []byte) error {
return nil
}

func New(s *auth.Session) *NAS {
return &NAS{
session: s,
address: auth.Address,
}
}

func (n *NAS) WithAddress(addr string) *NAS {
n.address = addr
return n
}

// ListDirectory would call FRITZ API and return the response structure with results
// or error.
func (n *NAS) ListDirectory() (*BrowseResponse, error) {
Expand All @@ -98,10 +110,10 @@ func (n *NAS) ListDirectory() (*BrowseResponse, error) {

// ListDirectoryWithParams is the same as ListDirectory, but accepts custom parameters
func (n *NAS) ListDirectoryWithParams(params map[string]string) (*BrowseResponse, error) {
fullAddress := fmt.Sprintf("%s/%s", n.Address, nasURIPath)
fullAddress := fmt.Sprintf("%s/%s", n.address, nasURIPath)

p := url.Values{}
p.Set("sid", n.SessionID.String())
p.Set("sid", n.session.String())
p.Set("sorting", "+filename")
p.Set("c", "files")
p.Set("a", "browse")
Expand Down Expand Up @@ -145,10 +157,10 @@ type CreateDirResponse struct {
}

func (n *NAS) CreateDir(name, path string) (*CreateDirResponse, error) {
fullAddress := fmt.Sprintf("%s/%s", n.Address, nasURIPath)
fullAddress := fmt.Sprintf("%s/%s", n.address, nasURIPath)

p := url.Values{}
p.Set("sid", n.SessionID.String())
p.Set("sid", n.session.String())
p.Set("path", path)
p.Set("name", name)
p.Set("parents", "false") // todo, find out
Expand Down Expand Up @@ -188,11 +200,11 @@ func (n *NAS) CreateDir(name, path string) (*CreateDirResponse, error) {

// GetFile downloads an object from FRITZ NAS storage
// Response is the object's data bytes (buffered) or error.
func (n *NAS) GetFile(sessionID string, path string) (io.ReadCloser, error) {
fullAddress := fmt.Sprintf("%s/%s", n.Address, nasFileGetPath)
func (n *NAS) GetFile(Session string, path string) (io.ReadCloser, error) {
fullAddress := fmt.Sprintf("%s/%s", n.address, nasFileGetPath)

p := url.Values{}
p.Add("sid", sessionID)
p.Add("sid", Session)
p.Add("script", fmt.Sprintf("/%s", rootAPI))
p.Add("c", "files")
p.Add("a", "get")
Expand Down Expand Up @@ -251,7 +263,7 @@ const (
)

func (n *NAS) PutFile(path string, data io.Reader) (*PutFileResponse, error) {
fullAddress := fmt.Sprintf("%s/%s", n.Address, nasFileUploadPath)
fullAddress := fmt.Sprintf("%s/%s", n.address, nasFileUploadPath)

// Parse path into file and dir
p := strings.Split(path, "/")
Expand All @@ -264,7 +276,7 @@ func (n *NAS) PutFile(path string, data io.Reader) (*PutFileResponse, error) {

// We need to insert this into the content type
params := make(map[string]string)
params["sid"] = n.SessionID.String()
params["sid"] = n.session.String()
params["dir"] = dir
for key, val := range params {
_ = writer.WriteField(key, val)
Expand Down Expand Up @@ -333,10 +345,10 @@ type RenameResponse struct {
// `to` takes only object's new filename, without the extension
// The function returns how many files were updated and an error if any
func (n *NAS) RenameFile(from, to string) (int, error) {
fullAddress := fmt.Sprintf("%s/%s", n.Address, nasURIPath)
fullAddress := fmt.Sprintf("%s/%s", n.address, nasURIPath)

p := url.Values{}
p.Add("sid", n.SessionID.String())
p.Add("sid", n.session.String())
p.Add("c", "files")
p.Add("a", "rename")
p.Add("paths[1][path]", from)
Expand Down Expand Up @@ -379,11 +391,11 @@ type DeleteResponse struct {

// DeleteObject will delete a file or directory from the NAS.
// Response contains how many files have been affected and error (if any).
func (n *NAS) DeleteObject(sessionID, path string) (int, error) {
fullAddress := fmt.Sprintf("%s/%s", n.Address, nasURIPath)
func (n *NAS) DeleteObject(Session, path string) (int, error) {
fullAddress := fmt.Sprintf("%s/%s", n.address, nasURIPath)

p := url.Values{}
p.Add("sid", sessionID)
p.Add("sid", Session)
p.Add("c", "files")
p.Add("a", "delete")
p.Add("paths[1]", path)
Expand Down Expand Up @@ -425,11 +437,11 @@ type MoveResponse struct {

// MoveFile moves a file from source to destination in the NAS
// This is a separate action and is not the same as rename.
func (n *NAS) MoveFile(sessionID, from, to string) (int, error) {
fullAddress := fmt.Sprintf("%s/%s", n.Address, nasURIPath)
func (n *NAS) MoveFile(Session, from, to string) (int, error) {
fullAddress := fmt.Sprintf("%s/%s", n.address, nasURIPath)

p := url.Values{}
p.Add("sid", sessionID)
p.Add("sid", Session)
p.Add("c", "files")
p.Add("a", "move")
p.Add("paths[1]", from)
Expand Down
2 changes: 1 addition & 1 deletion request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GenericGetRequest(url string) (*http.Response, error) {

// genericGetRequestWithContext is the same as genericGetRequest, but accepts context
func GenericGetRequestWithContext(ctx context.Context, url string) (*http.Response, error) {
// resp, err := http.Get(fmt.Sprintf("%s&sid=%s", url, sessionID))
// resp, err := http.Get(fmt.Sprintf("%s&sid=%s", url, Session))
rctx, cancel := context.WithTimeout(ctx, time.Duration(30)*time.Second)
defer cancel()

Expand Down

0 comments on commit ebd9eea

Please sign in to comment.