bykof / cordova-plugin-webserver

A webserver plugin for cordova
Other
119 stars 51 forks source link

iOS + Android file serving support #35

Closed boedy closed 5 years ago

boedy commented 5 years ago

@bykof Thanks for your work thus-far. Next to adding on-par functionality I made some changes in the setup.

This PR accomplishes:

Todo (upcoming PR's):

bykof commented 5 years ago

@boedy thank you very much for your Pull Request. I am honestly so busy right now, that I cannot maintain the project seriously, so I am really happy that there are guys like you!

I read through your pull requests and it looks pretty good. I will merge it and make you also a maintainer, so you can just push to the project.

boedy commented 4 years ago

Gotcha. I originally did not intend to help maintain the project, but I might as well as we'll be using this plugin in production. I'll see what I can do.

bykof commented 4 years ago

Could you tell me for what purpose you use this plugin in the application?

boedy commented 4 years ago

We use it to facilitate communications between a media device (i.e. smartspeaker) running on the local network and our app. We thereby respond to http request serving both media files and JSON responses.

digaus commented 4 years ago

@boedy

Any progress on "Method to return IP address of webserver where it can be reached on"?

Currently using Cordova Httpd plugin to serve zip files for ota updates of smarthome devices. For that I need to tell them the URL/IP which they need to pull the zip files from. I want to switch to this one because the httpd randomly crashes and is not maintained at all...

Maybe you can check here: https://github.com/floatinghotpot/cordova-httpd

It returns the IP of the server on startup

Android:

private String __getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (! inetAddress.isLoopbackAddress()) {
                        if (inetAddress instanceof Inet4Address) {
                            String ip = inetAddress.getHostAddress();
                            Log.w(LOGTAG, "local IP: "+ ip);
                            return ip;
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e(LOGTAG, ex.toString());
        }

        return "127.0.0.1";
    }

iOS:

- (NSString *)getIPAddress:(BOOL)preferIPv4
{
    NSArray *searchArray = preferIPv4 ?
    @[ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
    @[ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;

    NSDictionary *addresses = [self getIPAddresses];
    NSLog(@"addresses: %@", addresses);

    __block NSString *address;
    [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
     {
         address = addresses[key];
         if(address) *stop = YES;
     } ];
    return address ? address : IP_ANY;
}

- (NSDictionary *)getIPAddresses
{
    NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];

    // retrieve the current interfaces - returns 0 on success
    struct ifaddrs *interfaces;
    if(!getifaddrs(&interfaces)) {
        // Loop through linked list of interfaces
        struct ifaddrs *interface;
        for(interface=interfaces; interface; interface=interface->ifa_next) {
            if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
                continue; // deeply nested code harder to read
            }
            const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
            char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
            if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
                NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
                NSString *type;
                if(addr->sin_family == AF_INET) {
                    if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
                        type = IP_ADDR_IPv4;
                    }
                } else {
                    const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
                    if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
                        type = IP_ADDR_IPv6;
                    }
                }
                if(type) {
                    NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
                    addresses[key] = [NSString stringWithUTF8String:addrBuf];
                }
            }
        }
        // Free memory
        freeifaddrs(interfaces);
    }
    return [addresses count] ? addresses : nil;
}
boedy commented 4 years ago

@digaus Sorry have not yet had any time to work on this. I have a working branch where this already implemented. You can maybe use that for the time being.

The reason I'm waiting with a PR, is because I would like to get the versioning of this package strait first.

digaus commented 4 years ago

@digaus Sorry have not yet had any time to work on this. I have a working branch where this already implemented. You can maybe use that for the time being.

The reason I'm waiting with a PR, is because I would like to get the versioning of this package strait first.

No problem, I currently use WifiWizzard2 which gives me the option to get the IP. Also have an issue open for Ionic Native for some bugs there: https://github.com/ionic-team/ionic-native/issues/3126