Esempio n. 1
0
  @Override
  public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
    int clientId;
    HashMap<String, HashMap<String, String>> returnMap =
        new HashMap<String, HashMap<String, String>>();
    HashMap<String, String> returnKeyValues = new HashMap<String, String>();
    String strClientId;

    if (!doHTTPAuthentication(vhost, req, resp) || req.getMethod().equalsIgnoreCase("get")) return;

    req.parseBodyForParams();
    strClientId = req.getParameter("clientId");
    returnKeyValues.put("type", null);
    returnKeyValues.put("msg", "Request unsuccessful");
    returnKeyValues.put("isPublic", "true");
    returnMap.put("error", returnKeyValues);

    if (strClientId != null) {
      WMSLoggerFactory.getLogger(null)
          .info("*** Attempting to stop stream with clientId " + strClientId);
      try {
        clientId = Integer.parseInt(strClientId);
        IClient client = vhost.getClient(clientId);
        if (client != null) {
          client.setShutdownClient(true);
          returnMap.remove("error");
          returnKeyValues.remove("type");
          returnKeyValues.remove("msg");
          returnKeyValues.remove("isPublic");
          returnKeyValues.put("result", "Id " + clientId + " killed");
          returnMap.put("success", returnKeyValues);
        }
      } catch (NumberFormatException e) {
        WMSLoggerFactory.getLogger(null).error("******* NaN: " + strClientId);
        returnKeyValues.put("type", "NumberFormatException");
        returnKeyValues.put("msg", "NaN: " + strClientId);
        returnKeyValues.put("isPublic", "true");
        returnMap.put("error", returnKeyValues);
      }
    }

    JSONSerializer serializer = new JSONSerializer().exclude("*.class");
    String returnJSONStr = serializer.serialize(returnMap);

    try {
      resp.setHeader("Content-Type", "application/json");
      OutputStream out = resp.getOutputStream();
      byte[] outBytes = returnJSONStr.getBytes();
      out.write(outBytes);
    } catch (Exception e) {
      WMSLoggerFactory.getLogger(null).error("*** HTTPProvider failed: " + e.toString());
    }
  }
  public boolean checkPermissions(IClient client, RequestFunction function, AMFDataList params) {
    boolean doesStreamExist = false;
    boolean authorized = false;

    String streamName;
    try {
      streamName = params.getString(PARAM1).split("\\?")[0];
    } catch (Exception ex) {
      return false;
    }

    IMediaStream stream = client.getAppInstance().getStreams().getStream(streamName);

    getLogger().info("Checking stream Name: " + streamName);

    doesStreamExist = (stream != null);

    if (doesStreamExist) {
      authorized = false;
    } else {
      authorized = true;
    }

    // add other permission checking

    getLogger().info("Authorized: " + authorized);

    return authorized;
  }
  public void releaseStream(IClient client, RequestFunction function, AMFDataList params) {
    boolean bAuthorized = false;

    try {
      bAuthorized = checkPermissions(client, function, params);
    } catch (Exception ex) {
      // some error
    }

    if (bAuthorized != true) {
      sendClientOnStatusError(client, "NetStream.Publish.Denied", "Invalid credentials supplied");
      client.setShutdownClient(true);
    } else {
      invokePrevious(client, function, params);
    }
  }
 public void onConnect(IClient iclient, RequestFunction requestfunction, AMFDataList amfdatalist) {
   String s = iclient.getQueryStr();
   String s1 = "Unknown reason";
   String s2 = null;
   boolean flag = false;
   if (s == null) s1 = "Query string null";
   else if (s.length() == 0) {
     s1 = "Query string empty";
   } else {
     Map map = URLUtils.parseQueryStr(s, true);
     s2 = URLUtils.getParamValue(map, "secureToken");
     if (s2 == null)
       s1 =
           (new StringBuilder())
               .append("Query string mising secureToken (")
               .append(s)
               .append(")")
               .toString();
   }
   if (s2 == null && amfdatalist.size() > 3) {
     AMFData amfdata = amfdatalist.get(amfdatalist.size() - 1);
     if (amfdata.getType() == 2) {
       s2 = amfdatalist.getString(amfdatalist.size() - 1);
       s1 = "Unknown reason";
     }
   }
   if (s2 != null) {
     SecureTokenManager securetokenmanager = SecureTokenManager.getInstance(iclient.getVHost());
     Map map1 = securetokenmanager.getTokenList();
     SecureToken securetoken = (SecureToken) map1.get(s2);
     if (securetoken == null) {
       s1 =
           (new StringBuilder())
               .append("SecureToken not found (")
               .append(s2)
               .append(")")
               .toString();
     } else {
       int i = securetoken.incUsageCount();
       if (i == 1) {
         securetoken.setClient(iclient);
         securetokenmanager.registerClient(iclient, securetoken);
         iclient.acceptConnection("SecureToken valid");
         flag = true;
       } else {
         IClient iclient1 = securetoken.getClient();
         if (iclient1 != null) {
           s1 =
               (new StringBuilder())
                   .append("SecureToken already used by client (")
                   .append(iclient1.getClientId())
                   .append(")")
                   .toString();
           SecureTokenDef securetokendef = securetoken.getSecureTokenDef();
           if (securetokendef != null) {
             if (securetokendef.isDoKill())
               iclient1.getVHost().removeClient(iclient1.getClientId());
             if (securetokendef.isDoNotify()) {
               AMFDataObj amfdataobj = new AMFDataObj();
               amfdataobj.put("status", new AMFDataItem("error"));
               amfdataobj.put("reason", new AMFDataItem(s1));
               amfdataobj.put("clientId", new AMFDataItem(iclient.getClientId()));
               amfdataobj.put("ipAddress", new AMFDataItem(iclient.getIp()));
               amfdataobj.put("date", new AMFDataItem(new Date()));
               iclient1.call("onSecureToken", null, new Object[] {amfdataobj});
             }
           }
         } else {
           s1 =
               (new StringBuilder())
                   .append("SecureToken usage count is wrong (")
                   .append(securetoken.getUsageCount())
                   .append(")")
                   .toString();
         }
       }
     }
   }
   if (!flag) {
     iclient.rejectConnection(s1);
     // WMSLoggerFactory.getLogger(com/wowza/wms/plugin/security/ModuleSecureToken2).info((new
     // StringBuilder()).append("SecureToken: Connection refused
     // (").append(iclient.getClientId()).append("): ").append(s1).toString());
   }
 }
 public void onDisconnect(IClient iclient) {
   SecureTokenManager securetokenmanager = SecureTokenManager.getInstance(iclient.getVHost());
   securetokenmanager.unregisterClient(iclient);
 }