-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removing cling, adding wifi check to upnp check, moving upnp check to…
… settings
- Loading branch information
Showing
40 changed files
with
291 additions
and
935 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
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
130 changes: 125 additions & 5 deletions
130
common/src/main/java/org/syncloud/common/upnp/Router.java
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,13 +1,133 @@ | ||
package org.syncloud.common.upnp; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Optional; | ||
import com.google.common.base.Predicate; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
public interface Router { | ||
String getName(); | ||
import org.apache.log4j.Logger; | ||
import org.bitlet.weupnp.GatewayDevice; | ||
import org.bitlet.weupnp.PortMappingEntry; | ||
|
||
Optional<String> getExternalIP(); | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
int getPortMappingsCount(); | ||
import static com.google.common.collect.FluentIterable.from; | ||
|
||
boolean canManipulatePorts(String myIp); | ||
public class Router { | ||
|
||
private Logger logger = Logger.getLogger(Router.class); | ||
|
||
private GatewayDevice device; | ||
|
||
public Router(GatewayDevice device) { | ||
this.device = device; | ||
} | ||
|
||
public String getName() { | ||
return device.getModelName(); | ||
} | ||
|
||
public Optional<String> getExternalIP() { | ||
try { | ||
return Optional.of(device.getExternalIPAddress()); | ||
} catch (Exception e) { | ||
logger.error("unable to get external address, " + e.getMessage()); | ||
} | ||
return Optional.absent(); | ||
} | ||
|
||
public int getPortMappingsCount() { | ||
return getPortMappings().size(); | ||
} | ||
|
||
private List<PortMappingEntry> getPortMappings() { | ||
List<PortMappingEntry> mappings = new ArrayList<PortMappingEntry>(); | ||
int i = 0; | ||
while (true) { | ||
|
||
PortMappingEntry entry = new PortMappingEntry(); | ||
boolean exists = false; | ||
try { | ||
exists = device.getGenericPortMappingEntry(i, entry); | ||
} catch (Exception e) { | ||
logger.error("unable to get mapping: " + e.getMessage()); | ||
} | ||
|
||
if (exists) { | ||
mappings.add(entry); | ||
} else { | ||
break; | ||
} | ||
|
||
i++; | ||
} | ||
return mappings; | ||
} | ||
|
||
private Optional<Integer> getAvailableExternalPort() { | ||
ImmutableList<Integer> ports = from(getPortMappings()) | ||
.transform(new Function<PortMappingEntry, Integer>() { | ||
@Override | ||
public Integer apply(PortMappingEntry input) { | ||
return input.getExternalPort(); | ||
} | ||
}).toList(); | ||
|
||
for (int i = 10000; i < 65536; i++) { | ||
if (!ports.contains(i)) { | ||
return Optional.of(i); | ||
} | ||
} | ||
|
||
return Optional.absent(); | ||
} | ||
|
||
public boolean canManipulatePorts(String myIp) { | ||
final Optional<Integer> availableExternalPort = getAvailableExternalPort(); | ||
if(!availableExternalPort.isPresent()) | ||
return false; | ||
|
||
Integer port = availableExternalPort.get(); | ||
logger.info("first available port: " + port); | ||
|
||
PortMappingEntry portMapping = new PortMappingEntry(); | ||
portMapping.setExternalPort(port); | ||
portMapping.setInternalClient(myIp); | ||
portMapping.setProtocol("TCP"); | ||
|
||
try { | ||
if(!device.addPortMapping(port, port, myIp, "TCP", "syncloud")) | ||
return false; | ||
} catch (Exception e) { | ||
logger.error("unable to add mapping: " + port); | ||
return false; | ||
} | ||
|
||
Optional<PortMappingEntry> foundAdded = findPortMapping(port); | ||
if(!foundAdded.isPresent()) | ||
return false; | ||
|
||
logger.debug("found added: " + foundAdded.get().getExternalPort()); | ||
|
||
try { | ||
if(!device.deletePortMapping(port, "TCP")) | ||
return false; | ||
} catch (Exception e) { | ||
logger.error("unable to add mapping: " + port); | ||
return false; | ||
} | ||
|
||
return !findPortMapping(port).isPresent(); | ||
} | ||
|
||
private Optional<PortMappingEntry> findPortMapping(final int externalPort) { | ||
List<PortMappingEntry> portMappings = getPortMappings(); | ||
return from(portMappings).firstMatch(new Predicate<PortMappingEntry>() { | ||
@Override | ||
public boolean apply(PortMappingEntry input) { | ||
return input.getExternalPort() == externalPort; | ||
} | ||
}); | ||
} | ||
} |
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
18 changes: 0 additions & 18 deletions
18
common/src/main/java/org/syncloud/common/upnp/UPnPFactory.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.